textX / Arpeggio

Parser interpreter based on PEG grammars written in Python http://textx.github.io/Arpeggio/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Suppressing rules doesn't seem to work

igordejanovic opened this issue · comments

Discussed in #126

Originally posted by smurfix January 15, 2024
Suppose I want to ignore comment lines in my bibtex parser. So as per the documentation I add a class with suppress set.

diff --git bibtex.py bibtex.py
index 1801367..4332e31 100644
--- bibtex.py
+++ bibtex.py
@@ -14,6 +14,9 @@ import sys
 from arpeggio import *
 from arpeggio import RegExMatch as _

+class __(_):
+    suppress = True
+

 # Grammar
 def bibfile():                  return ZeroOrMore([comment_entry, bibentry, comment]), EOF
@@ -26,7 +29,7 @@ def fieldvalue_quotes():        return '"', fieldvalue_quoted_content, '"'

 # Lexical rules
 def fieldname():                return _(r'[-\w]+')
-def comment():                  return _(r'[^@]+')
+def comment():                  return __(r'[^@]+')
 def bibtype():                  return _(r'@\w+')
 def bibkey():                   return _(r'[^\s,]+')
 def fieldvalue_quoted_content():    return _(r'((\\")|[^"])*')

This should work (as I understand the documentation), but doesn't.

$ PYTHONPATH=.. python3 ./bibtex.py bibtex_example.bib
…
>> Matching rule bibfile=Sequence at position 0 => **** *** M6
   >> Matching rule ZeroOrMore in bibfile at position 0 => **** *** M6
      >> Matching rule OrderedChoice in bibfile at position 0 => **** *** M6
         >> Matching rule comment_entry=Sequence in bibfile at position 0 => **** *** M6
            ?? Try match rule StrMatch(@comment) in comment_entry at position 0 => **** *** M6
            -- No match '@comment' at 0 => '**** *** *M6'
         <<- Not matched rule comment_entry=Sequence in comment_entry at position 0 => **** *** M6
         >> Matching rule bibentry=Sequence in bibfile at position 0 => **** *** M6
            ?? Try match rule bibtype=RegExMatch(@\w+) in bibentry at position 0 => **** *** M6
            -- NoMatch at 0
         <<- Not matched rule bibentry=Sequence in bibentry at position 0 => **** *** M6
         ?? Try match rule comment=__([^@]+) in bibfile at position 0 => **** *** M6
         ++ Match '****** M6x
***

' at 0 => '**** *** M6x ***  *'
      <<- Not matched rule OrderedChoice in bibfile at position 0 => **** *** M6
   <<- Not matched rule ZeroOrMore in bibfile at position 0 => **** *** M6
   ?? Try match rule EOF in bibfile at position 0 => **** *** M6
   !! EOF not matched.
<<- Not matched rule bibfile=Sequence in bibfile at position 0 => **** *** M6
Traceback (most recent call last):
…
  File "/usr/lib/python3/dist-packages/arpeggio/__init__.py", line 907, in _parse
    parser._nm_raise(self, c_pos, parser)
  File "/usr/lib/python3/dist-packages/arpeggio/__init__.py", line 1727, in _nm_raise
    raise self.nm
arpeggio.NoMatch: Expected '@comment' or bibtype or EOF at position (1, 1) => '**** *** M6'.