Skip to content

Commit

Permalink
Refine grammar to avoid too much backtracking
Browse files Browse the repository at this point in the history
In order to provide useful error messages, the grammar had to be more
strict and not backtract too far from where the problem actually is.
  • Loading branch information
jmi2k committed Jun 13, 2020
1 parent 2459a4e commit 1a9e1f8
Showing 1 changed file with 42 additions and 42 deletions.
84 changes: 42 additions & 42 deletions learnbot_dsl/learnbotCode/Parser.py
Expand Up @@ -213,8 +213,8 @@ def used_vars(self):
+ Suppress(DOT)
+ IDENTIFIER
+ OPAR
+ Group(Optional(delimitedList(OPERATION)))
+ CPAR
- Group(Optional(delimitedList(OPERATION)))
- CPAR
).setParseAction(Call)

"""-----------------SIMPLECALL-------------------------"""
Expand All @@ -239,8 +239,8 @@ def used_vars(self):
SIMPLECALL = Group(
IDENTIFIER
+ OPAR
+ Group(Optional(delimitedList(OPERATION)))
+ CPAR
- Group(Optional(delimitedList(OPERATION)))
- CPAR
).setParseAction(SimpleCall)

"""-----------------OPERACIONES---------------------"""
Expand Down Expand Up @@ -355,7 +355,7 @@ def used_vars(self):
INDENT
+ IDENTIFIER
+ (ASSIGN | PLUA | MINA | DIVA | MULA)
+ OPERATION
- OPERATION
).setParseAction(Var)

"""-----------------LINEA---------------------------"""
Expand Down Expand Up @@ -454,26 +454,26 @@ def used_vars(self):
ELSEIF << (
INDENT
+ Suppress(Literal('elif'))
+ OPERATION
+ COLON
+ LINES
- OPERATION
- COLON
- LINES
).setParseAction(ElseIf)

ELSE << (
INDENT
+ Suppress(Literal('else'))
+ COLON
+ LINES
- COLON
- LINES
).setParseAction(Else)

IF = (
INDENT
+ Suppress(Literal('if'))
+ OPERATION
+ COLON
+ LINES
+ Group(ZeroOrMore(ELSEIF) + Optional(ELSE))
+ END
- OPERATION
- COLON
- LINES
- Group(ZeroOrMore(ELSEIF) + Optional(ELSE))
- END
).setParseAction(If)

"""-----------------LOOP----------------------------"""
Expand Down Expand Up @@ -509,10 +509,10 @@ def used_vars(self):
BLOQUEWHILE = (
INDENT
+ Suppress(Literal('while'))
+ OPERATION
+ COLON
+ LINES
+ END
- OPERATION
- COLON
- LINES
- END
).setParseAction(While)

"""-----------------WHEN+CONDICION------------------"""
Expand Down Expand Up @@ -577,14 +577,14 @@ def to_python(self, gen, *_):
BLOQUEWHENCOND = (
INDENT
+ Suppress(Literal('when'))
+ IDENTIFIER
+ Optional(
- IDENTIFIER
- Optional(
Suppress(ASSIGN)
+ OPERATION
- OPERATION
)
+ COLON
+ LINES
+ END
- COLON
- LINES
- END
).setParseAction(When)

"""-----------------ACTIVATE-CONDITION----------------"""
Expand Down Expand Up @@ -654,12 +654,12 @@ def used_vars(self):

DEF = (
Suppress(Literal('def'))
+ IDENTIFIER
+ OPAR
+ CPAR
+ COLON
+ LINES
+ END
- IDENTIFIER
- OPAR
- CPAR
- COLON
- LINES
- END
).setParseAction(Def)

"""-----------------IMPORT----------------------------"""
Expand Down Expand Up @@ -701,9 +701,9 @@ def used_vars(self):

MAIN = (
Suppress(Literal('main'))
+ COLON
+ LINES
+ END
- COLON
- LINES
- END
).setParseAction(Main)

class Program(Node):
Expand Down Expand Up @@ -731,9 +731,9 @@ def used_vars(self):

LB = (
Group(ZeroOrMore(IMPORT))
+ Group(ZeroOrMore(LINE))
+ Group(ZeroOrMore(DEF))
+ Group(MAIN | ZeroOrMore(BLOQUEWHENCOND))
- Group(ZeroOrMore(LINE))
- Group(ZeroOrMore(DEF))
- Group(MAIN | ZeroOrMore(BLOQUEWHENCOND))
).setParseAction(Program)

LB.ignore(pythonStyleComment)
Expand Down Expand Up @@ -850,8 +850,7 @@ def parserLearntBotCodeOnlyUserFuntion(code):
text = ""
# TODO: check for errors
try:
_, tree = Parser.parse_str(code)

tree = Parser.parse_str(code)
text = PythonGenerator.generate(tree)
except Exception as e:
traceback.print_exc()
Expand Down Expand Up @@ -882,7 +881,7 @@ def parserLearntBotCode(inputFile, outputFile, client_name):
errors.append({
'level': 'error',
'message': "Parse error",
'from': (output.lineno, output.col),
'from': (e.lineno, e.col),
'to': None,
})

Expand All @@ -909,7 +908,7 @@ def parserLearntBotCodeFromCode(code, name_client):
errors.append({
'level': 'error',
'message': "Parse error",
'from': (output.lineno, output.col),
'from': (e.lineno, e.col),
'to': None,
})

Expand All @@ -918,6 +917,7 @@ def parserLearntBotCodeFromCode(code, name_client):
if __name__ == "__main__":
textprueba = """
?
x = None
sum = None
result = None
Expand Down Expand Up @@ -975,7 +975,7 @@ def foo():
print()
print(PythonGenerator.generate(tree))
print()
except ParseException as pe:
except Exception as pe:
print(pe.line)
print(' ' * (pe.col - 1) + '^')
print(pe)
Expand Down

0 comments on commit 1a9e1f8

Please sign in to comment.