Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
A
arpeggio-gm
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
2
Merge Requests
2
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
郭家华
arpeggio-gm
Commits
f305a445
Commit
f305a445
authored
Aug 04, 2014
by
Igor Dejanovic
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Test for default semantic action
parent
0c82b67f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
68 additions
and
0 deletions
+68
-0
test_default_semantic_action.py
tests/unit/test_default_semantic_action.py
+68
-0
No files found.
tests/unit/test_default_semantic_action.py
0 → 100644
View file @
f305a445
# -*- coding: utf-8 -*-
#######################################################################
# Name: test_default_semantic_action
# Purpose: Default semantic action is applied during semantic analysis
# if no action is given for node type. Default action converts
# terminals to strings, remove StrMatch terminals from sequences.
# Author: Igor R. Dejanović <igor DOT dejanovic AT gmail DOT com>
# Copyright: (c) 2014 Igor R. Dejanović <igor DOT dejanovic AT gmail DOT com>
# License: MIT License
#######################################################################
import
pytest
from
arpeggio
import
ParserPython
,
SemanticAction
,
ParseTreeNode
from
arpeggio
import
RegExMatch
as
_
def
grammar
():
return
parentheses
,
'strmatch'
def
parentheses
():
return
'('
,
rulea
,
')'
def
rulea
():
return
[
'+'
,
'-'
],
number
def
number
():
return
_
(
r'\d+'
)
p_removed
=
False
number_str
=
False
parse_tree_node
=
False
class
ParenthesesSA
(
SemanticAction
):
def
first_pass
(
self
,
parser
,
node
,
children
):
global
p_removed
,
parse_tree_node
p_removed
=
str
(
children
[
0
])
!=
'('
parse_tree_node
=
isinstance
(
children
[
0
],
ParseTreeNode
)
return
children
[
0
]
if
len
(
children
)
==
1
else
children
[
1
]
class
RuleSA
(
SemanticAction
):
def
first_pass
(
self
,
parser
,
node
,
children
):
global
number_str
number_str
=
type
(
children
[
1
])
==
str
return
children
[
1
]
parentheses
.
sem
=
ParenthesesSA
()
rulea
.
sem
=
RuleSA
()
def
test_default_action_enabled
():
parser
=
ParserPython
(
grammar
)
parse_tree
=
parser
.
parse
(
'(-34) strmatch'
)
parser
.
getASG
(
defaults
=
True
)
assert
p_removed
assert
number_str
assert
not
parse_tree_node
def
test_default_action_disabled
():
parser
=
ParserPython
(
grammar
)
parse_tree
=
parser
.
parse
(
'(-34) strmatch'
)
parser
.
getASG
(
defaults
=
False
)
assert
not
p_removed
assert
not
number_str
assert
parse_tree_node
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment