Selaa lähdekoodia

Added support for all RE flags.

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.5.1
Erez Shinan 6 vuotta sitten
vanhempi
commit
ad30c324f5
4 muutettua tiedostoa jossa 9 lisäystä ja 7 poistoa
  1. +2
    -2
      examples/python2.g
  2. +1
    -1
      examples/python3.g
  3. +5
    -3
      lark/load_grammar.py
  4. +1
    -1
      lark/tools/nearley.py

+ 2
- 2
examples/python2.g Näytä tiedosto

@@ -145,11 +145,11 @@ number: DEC_NUMBER | HEX_NUMBER | OCT_NUMBER | FLOAT | IMAG_NUMBER
string: STRING | LONG_STRING string: STRING | LONG_STRING
// Tokens // Tokens


COMMENT: /\#[^\n]*/
COMMENT: /#[^\n]*/
_NEWLINE: ( /\r?\n[\t ]*/ | COMMENT )+ _NEWLINE: ( /\r?\n[\t ]*/ | COMMENT )+


%ignore /[\t \f]+/ // WS %ignore /[\t \f]+/ // WS
%ignore /\\\\[\t \f]*\r?\n/ // LINE_CONT
%ignore /\\[\t \f]*\r?\n/ // LINE_CONT
%ignore COMMENT %ignore COMMENT






+ 1
- 1
examples/python3.g Näytä tiedosto

@@ -170,7 +170,7 @@ _NEWLINE: ( /\r?\n[\t ]*/ | COMMENT )+




%ignore /[\t \f]+/ // WS %ignore /[\t \f]+/ // WS
%ignore /\\\\[\t \f]*\r?\n/ // LINE_CONT
%ignore /\\[\t \f]*\r?\n/ // LINE_CONT
%ignore COMMENT %ignore COMMENT






+ 5
- 3
lark/load_grammar.py Näytä tiedosto

@@ -18,6 +18,8 @@ from .tree import Tree as T, Transformer, InlineTransformer, Visitor
__path__ = os.path.dirname(__file__) __path__ = os.path.dirname(__file__)
IMPORT_PATHS = [os.path.join(__path__, 'grammars')] IMPORT_PATHS = [os.path.join(__path__, 'grammars')]


_RE_FLAGS = 'imslux'

_TOKEN_NAMES = { _TOKEN_NAMES = {
'.' : 'DOT', '.' : 'DOT',
',' : 'COMMA', ',' : 'COMMA',
@@ -70,7 +72,7 @@ TOKENS = {
'RULE': '!?[_?]?[a-z][_a-z0-9]*', 'RULE': '!?[_?]?[a-z][_a-z0-9]*',
'TOKEN': '_?[A-Z][_A-Z0-9]*', 'TOKEN': '_?[A-Z][_A-Z0-9]*',
'STRING': r'"(\\"|\\\\|[^"\n])*?"i?', 'STRING': r'"(\\"|\\\\|[^"\n])*?"i?',
'REGEXP': r'/(?!/)(\\/|\\\\|[^/\n])*?/i?',
'REGEXP': r'/(?!/)(\\/|\\\\|[^/\n])*?/[%s]?' % _RE_FLAGS,
'_NL': r'(\r?\n)+\s*', '_NL': r'(\r?\n)+\s*',
'WS': r'[ \t]+', 'WS': r'[ \t]+',
'COMMENT': r'//[^\n]*', 'COMMENT': r'//[^\n]*',
@@ -287,7 +289,7 @@ class ExtractAnonTokens(InlineTransformer):


def _literal_to_pattern(literal): def _literal_to_pattern(literal):
v = literal.value v = literal.value
if v[-1] in 'i':
if v[-1] in _RE_FLAGS:
flags = v[-1] flags = v[-1]
v = v[:-1] v = v[:-1]
else: else:
@@ -295,7 +297,7 @@ def _literal_to_pattern(literal):


assert v[0] == v[-1] and v[0] in '"/' assert v[0] == v[-1] and v[0] in '"/'
x = v[1:-1] x = v[1:-1]
x = re.sub(r'(\\[wd/]|\\\[|\\\])', r'\\\1', x)
x = re.sub(r'(\\[wd/ ]|\\\[|\\\])', r'\\\1', x)
x = x.replace("'", r"\'") x = x.replace("'", r"\'")
s = literal_eval("u'''%s'''" % x) s = literal_eval("u'''%s'''" % x)
return { 'STRING': PatternStr, return { 'STRING': PatternStr,


+ 1
- 1
lark/tools/nearley.py Näytä tiedosto

@@ -26,7 +26,7 @@ nearley_grammar = r"""
rule: NAME rule: NAME
string: STRING string: STRING
regexp: REGEXP regexp: REGEXP
JS: /(?s){%.*?%}/
JS: /{%.*?%}/s
js: JS? js: JS?


NAME: /[a-zA-Z_$]\w*/ NAME: /[a-zA-Z_$]\w*/


Ladataan…
Peruuta
Tallenna