1 from .SPDGrammarLexer
import SPDGrammarLexer
2 from .SPDGrammarParser
import SPDGrammarParser
3 from .SPDGrammarVisitor
import SPDGrammarVisitor
4 from antlr4
import PredictionMode
5 from antlr4.CommonTokenStream
import CommonTokenStream
6 from antlr4.error.ErrorListener
import ConsoleErrorListener
7 from antlr4.error.Errors
import RecognitionException
8 from antlr4.error.ErrorStrategy
import BailErrorStrategy, DefaultErrorStrategy
9 from antlr4.InputStream
import InputStream
16 def __init__(self, actions):
21 def visitPair(self, ctx):
22 string = ctx.STRING().getText()
23 valueContext = ctx.value()
25 if self.
_status == 0
and string ==
'"status"':
26 self.
_status = SPDGrammarVisitorImplementation.OK_STATUS
if (
27 valueContext.getText() ==
'"ok"')
else SPDGrammarVisitorImplementation.ERROR_STATUS
28 elif self.
_status == SPDGrammarVisitorImplementation.ERROR_STATUS:
29 if string ==
'"result"':
35 return self.visitChildren(ctx)
37 self.
_errors += SPDGrammarVisitorImplementation._trim(
38 valueContext.getText())
39 elif string ==
'"error"':
40 self.
_errors += SPDGrammarVisitorImplementation._trim(
41 valueContext.getText())
42 elif self.
_status == SPDGrammarVisitorImplementation.OK_STATUS:
43 if string ==
'"name"':
45 SPDGrammarVisitorImplementation._trim(
46 valueContext.getText()))
47 elif string ==
'"plan"' or string ==
'"result"':
48 return self.visitChildren(ctx)
52 return string[1:-1]
if (string[0] ==
53 '"' and string[-1] ==
'"')
else string
56 def parse(actions, spdOutput, two_stageParsing):
61 if not two_stageParsing:
62 visitor.visit(parser.json())
64 return visitor._errors
66 parser._interp.predictionMode = PredictionMode.SLL
67 parser.removeErrorListeners()
68 parser._errHandler = BailErrorStrategy()
71 visitor.visit(parser.json())
72 except RuntimeError
as exception:
73 if isinstance(exception, RecognitionException):
75 parser.addErrorListener(ConsoleErrorListener.INSTANCE)
76 parser._errHandler = DefaultErrorStrategy()
77 parser._interp.predictionMode = PredictionMode.LL
78 visitor.visit(parser.json())
80 return visitor._errors