Commits

Juancarlo Añez  committed 6fd87ae

Update the antlr2grako example to deal with new features in ANTLR 4.

  • Participants
  • Parent commits 162ce84

Comments (0)

Files changed (5)

File examples/antlr2grako/grammar/antlr.ebnf

     =
       named
     | predicate_or_action
+    | optional
     | closure
     | positive_closure
-    | optional
     | atom
     ;
 
 
 optional
     =
-    @atom '?' >>
+    @(closure|positive_closure|atom) '?' >>
     ;
 
 closure
     | negative
     | syntactic_predicate
     | subexp
+    | newrange
     | range
     | terminal
     | non_terminal
     )
     ['^'|'!']
+    [annotation]
+    ;
+
+annotation
+    =
+    '<' id '=' id '>'
     ;
 
 eof
     first:char '..' >> last:char
     ;
 
+newrange
+    =
+    NEWRANGE
+    ;
+
+NEWRANGE
+    =
+    '[' @?/([^\]]|\\])+/? ']'
+    ;
+
 subexp
     =
     '('

File examples/antlr2grako/semantics.py

 # -*- coding: utf-8 -*-
 from __future__ import print_function, division, absolute_import, unicode_literals
+import re
 from grako import grammars as model
 
 
     def range(self, ast):
         return model.Pattern('[%s-%s]' % (ast.first, ast.last))
 
+    def newrange(self, ast):
+        return model.Pattern('[%s]' % re.escape(ast))
+
     def non_terminal(self, ast):
         return model.RuleRef(ast)
 

File grako/bootstrap.py

             with self._option():
                 self._token('"')
                 self._cut()
-                self._pattern(r'([^"\\\n]|\\"|\\\\)*')
+                self._pattern(r'([^"\n]|\\"|\\\\)*')
                 self.ast['@'] = self.last_node
                 self._token('"')
             with self._option():
                 self._token("'")
                 self._cut()
-                self._pattern(r"([^'\\\n]|\\'|\\\\)*")
+                self._pattern(r"([^'\n]|\\'|\\\\)*")
                 self.ast['@'] = self.last_node
                 self._token("'")
             self._error('expecting one of: \' "')

File grako/buffering.py

                                    and self.is_name_char()
                                    )
             if check_nameguard:
-                return token
+                return str(token)
         self.goto(p)
 
     def matchre(self, pattern, ignorecase=None):
         if matched:
             token = matched.group()
             self._pos += len(token)
-            return token
+            return str(token)
 
     def get_fileinfo(self, text, filename):
         return [filename] * len(text.splitlines())

File grako/contexts.py

 
     def _trace(self, msg, *params):
         if self.trace:
-            print(unicode(msg % params).encode(self.encoding), file=sys.stderr)
+            print(msg % params, file=sys.stderr)
 
     def _trace_event(self, event):
         if self.trace: