Skip to content
This repository has been archived by the owner on Oct 16, 2023. It is now read-only.

Commit

Permalink
fix: new error listener that only bypass tab problems. Flama will inf…
Browse files Browse the repository at this point in the history
…orm about bad constraints
  • Loading branch information
jagalindo committed Aug 23, 2023
1 parent fe9a0af commit df39710
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion uvlparser/main.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,62 @@
from antlr4 import CommonTokenStream, FileStream
from uvlparser.UVLLexer import UVLLexer
from uvlparser.UVLParser import UVLParser

from antlr4.error.ErrorListener import ErrorListener
from flamapy.core.exceptions import ParsingException
class CustomErrorListener(ErrorListener):
def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
# If the error message contains a specific keyword related to tabulation, ignore it
if "\\t" in msg:
return
else:
# Otherwise, print the error (or handle it in another way)
print(f"Line {line}:{column} - {msg}")
raise ParsingException("The UVL has sintactic problems that prevents reading it")


def get_tree(argv):
input_stream = FileStream(argv)
lexer = UVLLexer(input_stream)

# Attach the custom error listener to the lexer
lexer.removeErrorListeners()
lexer.addErrorListener(CustomErrorListener())

stream = CommonTokenStream(lexer)
parser = UVLParser(stream)

# Attach the custom error listener to the parser
parser.removeErrorListeners()
parser.addErrorListener(CustomErrorListener())

tree = parser.feature_model()

return tree


if __name__ == "__main__":
import sys

# Check if the user provided a file argument
if len(sys.argv) < 2:
print("Usage: python script_name.py <path_to_uvl_file>")
sys.exit(1)

# Parse the provided file
input_stream = FileStream(sys.argv[1])
lexer = UVLLexer(input_stream)

# Attach the custom error listener to the lexer
lexer.removeErrorListeners()
lexer.addErrorListener(CustomErrorListener())

stream = CommonTokenStream(lexer)
parser = UVLParser(stream)

# Attach the custom error listener to the parser
parser.removeErrorListeners()
parser.addErrorListener(CustomErrorListener())

tree = parser.feature_model()
# Print the parse tree (optional)
print(tree.toStringTree(recog=parser))

0 comments on commit df39710

Please sign in to comment.