1
0
Fork 0

allow parser syntax errors to stop parser tree walking

This commit is contained in:
Kevin Matz 2018-10-28 19:21:19 -10:00
parent af2bd818da
commit f38e5c77ee
2 changed files with 14 additions and 6 deletions

View File

@ -177,11 +177,7 @@ class OscCommentMacroListener(CommentMacroListener):
if ctx.target()is None:
logger.error("ERROR: limited to fading specified masters only.")
return
try:
level = ctx.number().value
except AttributeError:
logger.error("ERROR: level arument is required!")
return
level = ctx.number().value
if (level < 0 or level > 100):
logger.error("Level must be between 0 and 100.")
return

View File

@ -20,12 +20,19 @@ import configparser
import logging
import sys
from antlr4.error.ErrorListener import ErrorListener
from CommentMacroLexer import CommentMacroLexer
from CommentMacroParser import CommentMacroParser
from OscCommentMacroListener import OscCommentMacroListener
from pythonosc import udp_client
# define an error listener that raises SyntaxError exceptions
class SyntaxErrorListener(ErrorListener):
def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
raise SyntaxError('line ' + str(line) + ': ' + msg)
# setup logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
@ -50,7 +57,12 @@ def comment(text):
lexer = CommentMacroLexer(input_stream)
stream = antlr4.CommonTokenStream(lexer)
parser = CommentMacroParser(stream)
tree = parser.prog()
parser._listeners = [SyntaxErrorListener()]
try:
tree = parser.prog()
except SyntaxError as e:
logger.error(e)
return
oscMacro = OscCommentMacroListener(servers)
walker = antlr4.ParseTreeWalker()
walker.walk(oscMacro, tree)