Skip to content

Commit

Permalink
Merge 2b91701 into ce0123f
Browse files Browse the repository at this point in the history
  • Loading branch information
goto40 committed Sep 30, 2019
2 parents ce0123f + 2b91701 commit ad7cafa
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/grammar.md
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,10 @@ specified in brackets (`[ ]`) at the beginning of the rule's definition after
the rule's name. Currently, they are used to alter parser configuration for
whitespace handling on the rule level.

Rule modifiers act on the current rule and all rules referenced inside the rule
(recursively): unless a refrenced rule has an explicit rule modifier, the currently
active modifier state is propagated to referenced rules.

There are two rule modifiers at the moment:

* **skipws, noskipws** - are used to enable/disable whitespace skipping during
Expand Down
84 changes: 84 additions & 0 deletions tests/functional/regressions/test_issue205_skipws_propagation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from __future__ import unicode_literals
from textx.metamodel import metamodel_from_str


def test_issue205_skipws_propagation1():
"""
Here, we show how the ```skipws``` rule modifier is propagated
to all rules used by the rule with the modifier.
"""
mm = metamodel_from_str('''
SGroup[skipws]: '<s>' Sentence '</s>'; // skipws active
Sentence: /(?i) */ words*=Word /(?i) */; // skipws active <- SGroup
Word: /(?i) */ ('bar' '.' | 'bar' | '.'); // skipws active <- SGroup
''', skipws=False, debug=False)

m = mm.model_from_str('''<s> bar . </s>''', debug=False)
assert 1 == len(m.words)
assert 'bar.' == m.words[0]


def test_issue205_skipws_propagation2():
"""
Here, we show how the default ```skipws``` rule modifier is used
as default (because no modifier is explicitly specified).
"""
mm = metamodel_from_str('''
SGroup: '<s>' Sentence '</s>';
Sentence: /(?i) */ words*=Word /(?i) */;
Word: /(?i) */ ('bar' '.' | 'bar' | '.');
''',
skipws=False, # default skipws value
debug=False)

assert 2 == len(mm.model_from_str('''<s> bar . </s>''', debug=False).words)
assert 1 == len(mm.model_from_str('''<s> bar. </s>''', debug=False).words)


def test_issue205_skipws_propagation3():
"""
Here, we show how a given ```skipws``` rule modifier is overridden
by another explicitly specified rule modifier, which stops the
propagation: the ```skipws``` rule modifier in ```SGroup``` is
overridden by the ```noskipws``` rule modifier of the
rule ```Sentence``` (used in ```SGroup```).
"""
mm = metamodel_from_str('''
SGroup[skipws]: '<s>' Sentence '</s>';
Sentence[noskipws]: /(?i) */ words*=Word /(?i) */;
Word: /(?i) */ ('bar' '.' | 'bar' | '.');
''', skipws=False, debug=False)

assert 2 == len(mm.model_from_str('''<s> bar . </s>''', debug=False).words)
assert 1 == len(mm.model_from_str('''<s> bar. </s>''', debug=False).words)
assert 1 == len(mm.model_from_str('''<s> bar </s>''', debug=False).words)
assert 1 == len(mm.model_from_str('''<s> . </s>''', debug=False).words)


def test_issue205_skipws_propagation4():
"""
Here, we show that a rule with the ```noskipws``` rule modifier
is able to consume spaces explicitly. Match suppression (-) is
employed to exclude these spaces from the target value (this means the
```Word``` does not include the spaces in its value).
Note: ```skipws``` of Sentence does not consume the spaces between
the words, because the [rule modifiers]
(http://textx.github.io/textX/stable/grammar/#rule-modifiers)
apply immediately. Thus, we need to consume the extra spaces before a word.
"""
mm = metamodel_from_str(r'''
Sentence[skipws]: words*=Word;
Word[noskipws]: /\s*/- ((ID '.') | ID | '.');
''', skipws=True)

m = mm.model_from_str('''foo bar .''')
assert "foo" == m.words[0]
assert "bar" == m.words[1]
assert 3 == len(m.words)
assert "." == m.words[2]

m = mm.model_from_str('''foo bar.''')
assert "foo" == m.words[0]
assert "bar." == m.words[1]
assert 2 == len(m.words)

0 comments on commit ad7cafa

Please sign in to comment.