This repository has been archived by the owner on Oct 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: new error listener that only bypass tab problems. Flama will inf…
…orm about bad constraints
- Loading branch information
Showing
1 changed file
with
48 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |