Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
referencedParameter = (Suppress(Keyword("parameter")) + variableRef + Suppress(Keyword("references")) + qName
+ separator).setParseAction(compileParameterReference).ignore(xfsComment).setName("renamed-parameter").setDebug(debugParsing)
precondition = ( Suppress(Keyword("precondition")) + xpathExpression + separator
).setParseAction(compilePrecondition).ignore(xfsComment).setName("precondition").setDebug(debugParsing)
formula = ( Suppress(Keyword("formula")) + ncName + Suppress(Literal("{")) +
ZeroOrMore( label | severity |
Keyword("aspect-model-non-dimensional") +separator |
Keyword("no-implicit-filtering") + separator |
Keyword("decimals") + xpathExpression + separator |
Keyword("precision") + xpathExpression + separator |
Keyword("value") + xpathExpression + separator |
Keyword("source") + qName + separator ) +
ZeroOrMore( aspectRules ) +
ZeroOrMore( filter ) +
ZeroOrMore( generalVariable | factVariable | referencedParameter) +
ZeroOrMore( precondition ) +
Suppress(Literal("}") + separator)
).setParseAction(compileFormula).ignore(xfsComment).setName("formula").setDebug(debugParsing)
assertion = ( Suppress(Keyword("assertion")) + ncName + Suppress(Literal("{")) +
ZeroOrMore( label | severity |
Keyword("aspect-model-non-dimensional") +separator |
Keyword("no-implicit-filtering") + separator ) +
ZeroOrMore( filter ) +
ZeroOrMore( generalVariable | factVariable | referencedParameter) +
ZeroOrMore( precondition ) +
Optional( ( Keyword("test") | Keyword("evaluation-count") ) + xpathExpression + separator) +
Suppress(Literal("}") + separator)).setParseAction(compileAssertion).ignore(xfsComment).setName("assertion").setDebug(debugParsing)
consistencyAssertion = ( Suppress(Keyword("consistency-assertion")) + ncName + Suppress(Literal("{")) +
nil = pp.CaselessKeyword('nil').addParseAction(pp.replaceWith([]))
t = pp.CaselessKeyword('t').addParseAction(pp.replaceWith(True))
constant = styler('class:constant', nil | t)
number = styler('class:number', ppc.number).setName('number')
control_chars = ''.join(map(chr, range(0, 32))) + '\x7f'
symbol = pp.CharsNotIn(control_chars + '\'"`;,()[]{} ')
symbol = styler('class:symbol', symbol).setName('symbol')
symbol.addParseAction(lambda t: Symbol(t[0]))
call = styler('class:call', symbol)
string = DQUO + pp.Combine(pp.Optional(pp.CharsNotIn('"'))) + cond_optional(DQUO)
string = styler('class:string', string).setName('string')
forms = (form_first + pp.ZeroOrMore(form)).setName('one or more forms')
sexp = (LPAR + pp.Optional(forms) + cond_optional(RPAR)).setName('s-expression')
sexp.addParseAction(lambda t: [list(t)])
quote = (styler('class:quote', SQUO) + form).setName('quoted form')
quote.addParseAction(lambda t: Quote(t[0]))
form_first <<= constant | number ^ call | string | sexp | quote
form <<= constant | number ^ symbol | string | sexp | quote
return form
grace_stem = Optional (decorations) + stem
grace_notes << Group (Suppress ('{') + Optional (acciaccatura) + OneOrMore (grace_stem) + Suppress ('}'))
text_expression = Optional (oneOf ('^ _ < > @'), '^') + Optional (CharsNotIn ('"'), "")
chord_accidental = oneOf ('# b =')
triad = oneOf ('ma Maj maj M mi min m aug dim o + -')
seventh = oneOf ('7 ma7 Maj7 M7 maj7 mi7 m7 dim7 o7 -7 aug7 +7 m7b5 mi7b5')
sixth = oneOf ('6 ma6 M6 m6 mi6')
ninth = oneOf ('9 ma9 M9 maj9 Maj9 mi9 m9')
elevn = oneOf ('11 ma11 M11 maj11 Maj11 mi m11')
suspended = oneOf ('sus sus2 sus4')
chord_degree = Combine (Optional (chord_accidental) + oneOf ('2 4 5 6 7 9 11 13'))
chord_kind = Optional (seventh | sixth | ninth | elevn | triad, '_') + Optional (suspended)
chord_root = oneOf ('C D E F G A B') + Optional (chord_accidental)
chord_bass = oneOf ('C D E F G A B') + Optional (chord_accidental) # needs a different parse action
chordsym = chord_root + chord_kind + ZeroOrMore (chord_degree) + Optional (Suppress ('/') + chord_bass)
chord_sym = chordsym + Optional (Literal ('(') + CharsNotIn (')') + Literal (')')).suppress ()
chord_or_text = Suppress ('"') + (chord_sym ^ text_expression) + Suppress ('"')
volta_nums = Optional ('[').suppress () + Combine (Word (nums) + ZeroOrMore (oneOf (', -') + Word (nums)))
volta_text = Literal ('[').suppress () + Regex (r'"[^"]+"')
volta = volta_nums | volta_text
invisible_barline = oneOf ('[|] []')
dashed_barline = oneOf (': .|')
double_rep = Literal (':') + FollowedBy (':') # otherwise ambiguity with dashed barline
voice_overlay = Combine (OneOrMore ('&'))
bare_volta = FollowedBy (Literal ('[') + Word (nums)) # no barline, but volta follows (volta is parsed in next measure)
bar_left = (oneOf ('[|: |: [: :') + Optional (volta)) | Optional ('|').suppress () + volta | oneOf ('| [|')
bars = ZeroOrMore (':') + ZeroOrMore ('[') + OneOrMore (oneOf ('| ]'))
bar_right = invisible_barline | double_rep | Combine (bars) | dashed_barline | voice_overlay | bare_volta
errors = ~bar_right + Optional (Word (' \n')) + CharsNotIn (':&|', exact=1)
referencedParameter = (Suppress(Keyword("parameter")) + variableRef + Suppress(Keyword("references")) + qName
+ separator).setParseAction(compileParameterReference).ignore(xfsComment).setName("renamed-parameter").setDebug(debugParsing)
precondition = ( Suppress(Keyword("precondition")) + xpathExpression + separator
).setParseAction(compilePrecondition).ignore(xfsComment).setName("precondition").setDebug(debugParsing)
formula = ( Suppress(Keyword("formula")) + ncName + Suppress(Literal("{")) +
ZeroOrMore( label | severity |
Keyword("aspect-model-non-dimensional") +separator |
Keyword("no-implicit-filtering") + separator |
Keyword("decimals") + xpathExpression + separator |
Keyword("precision") + xpathExpression + separator |
Keyword("value") + xpathExpression + separator |
Keyword("source") + qName + separator ) +
ZeroOrMore( aspectRules ) +
ZeroOrMore( filter ) +
ZeroOrMore( generalVariable | factVariable | referencedParameter) +
ZeroOrMore( precondition ) +
Suppress(Literal("}") + separator)
).setParseAction(compileFormula).ignore(xfsComment).setName("formula").setDebug(debugParsing)
assertion = ( Suppress(Keyword("assertion")) + ncName + Suppress(Literal("{")) +
ZeroOrMore( label | severity |
Keyword("aspect-model-non-dimensional") +separator |
Keyword("no-implicit-filtering") + separator ) +
ZeroOrMore( filter ) +
ZeroOrMore( generalVariable | factVariable | referencedParameter) +
ZeroOrMore( precondition ) +
Optional( ( Keyword("test") | Keyword("evaluation-count") ) + xpathExpression + separator) +
Suppress(Literal("}") + separator)).setParseAction(compileAssertion).ignore(xfsComment).setName("assertion").setDebug(debugParsing)
quote = Suppress(Word("\""))
op = Suppress(Word("{"))
cl = Suppress(Word("}"))
opp = Suppress(Word("("))
clp = Suppress(Word(")"))
lt = Suppress(Word("<"))
gt = Suppress(Word(">"))
eq = Suppress(Word("="))
identifier = Word(alphas+"_",alphanums+"_")
typeIdentifier = Word(alphas+"_",alphanums+"_:")
structIdentifer = Group(typeIdentifier.setResultsName('type') + identifier.setResultsName('identifier') + Optional(eq) + Optional(CharsNotIn(";").setResultsName('defaultValue')) + semicolon)
structIdentifers = Group(OneOrMore(structIdentifer))
## Imports
idslImport = Suppress(Word("import")) + quote + CharsNotIn("\";").setResultsName('path') + quote + semicolon
idslImports = ZeroOrMore(idslImport)
structDef = Word("struct").setResultsName('type') + identifier.setResultsName('name') + op + structIdentifers.setResultsName("structIdentifiers") + cl + semicolon
dictionaryDef = Word("dictionary").setResultsName('type') + lt + CharsNotIn("<>").setResultsName('content') + gt + identifier.setResultsName('name') + semicolon
sequenceDef = Word("sequence").setResultsName('type') + lt + typeIdentifier.setResultsName('typeSequence') + gt + identifier.setResultsName('name') + semicolon
enumDef = Word("enum").setResultsName('type') + identifier.setResultsName('name') + op + CharsNotIn("{}").setResultsName('content') + cl + semicolon
exceptionDef = Word("exception").setResultsName('type') + identifier.setResultsName('name') + op + CharsNotIn("{}").setResultsName('content') + cl + semicolon
raiseDef = Suppress(Word("throws")) + typeIdentifier + ZeroOrMore( Literal(',') + typeIdentifier )
decoratorDef = Literal('idempotent') | Literal('out')
retValDef = typeIdentifier.setResultsName('ret')
firstParam = Group( Optional(decoratorDef.setResultsName('decorator')) + typeIdentifier.setResultsName('type') + identifier.setResultsName('name'))
nextParam = Suppress(Word(',')) + firstParam
params = firstParam + ZeroOrMore(nextParam)
+-------+------+------+------+------+------+------+------+------+
"""
# define grammar for datatable
underline = Word("-=")
number = Word(nums).setParseAction( lambda t : int(t[0]) )
vert = Literal("|").suppress()
rowDelim = ("+" + ZeroOrMore( underline + "+" ) ).suppress()
columnHeader = Group(vert + vert + delimitedList(Word(alphas + nums), "|") + vert)
heading = rowDelim + columnHeader.setResultsName("columns") + rowDelim
rowData = Group( vert + Word(alphas) + vert + delimitedList(number,"|") + vert )
trailing = rowDelim
datatable = heading + Dict( ZeroOrMore(rowData) ) + trailing
# now parse data and print results
data = datatable.parseString(testData)
print(data)
print(data.asXML("DATA"))
pprint.pprint(data.asList())
print("data keys=", list(data.keys()))
print("data['min']=", data['min'])
print("sum(data['min']) =", sum(data['min']))
print("data.max =", data.max)
print("sum(data.max) =", sum(data.max))
# now print transpose of data table, using column labels read from table header and
# values from data lists
print()
print(" " * 5, end=' ')
atom = Group(rangeNotPython + Optional(unary_op)("op")) | terminal | (notSet + Optional(unary_op)("op")) | (RULE_REF + Optional(ARG_ACTION("arg")) + Optional(unary_op)("op"))
element = Forward()
treeSpec = Suppress('^(') + element*(2,) + Suppress(')')
ebnfSuffix = oneOf("? * +")
ebnf = block + Optional(ebnfSuffix("op") | '=>')
elementNoOptionSpec = (id("result_name") + oneOf('= +=')("labelOp") + atom("atom") + Optional(ebnfSuffix)) | (id("result_name") + oneOf('= +=')("labelOp") + block + Optional(ebnfSuffix)) | atom("atom") + Optional(ebnfSuffix) | ebnf | ACTION | (treeSpec + Optional(ebnfSuffix)) # | SEMPRED ( '=>' -> GATED_SEMPRED | -> SEMPRED )
element << Group(elementNoOptionSpec)("element")
alternative = Group(Group(OneOrMore(element))("elements")) # Do not ask me why group is needed twice... seems like the xml that you see is not always the real structure?
rewrite = Optional(Literal('TODO REWRITE RULES TODO'))
block << Suppress('(') + Optional(Optional(optionsSpec("opts")) + Suppress(':')) + Group(alternative('a1') + rewrite + Group(ZeroOrMore(Suppress('|') + alternative('a2') + rewrite))("alternatives"))("block") + Suppress(')')
altList = alternative('a1') + rewrite + Group(ZeroOrMore(Suppress('|') + alternative('a2') + rewrite))("alternatives")
exceptionHandler = Suppress('catch') + ARG_ACTION + ACTION
finallyClause = Suppress('finally') + ACTION
exceptionGroup = (OneOrMore(exceptionHandler) + Optional(finallyClause)) | finallyClause
ruleHeading = Optional(ML_COMMENT)("ruleComment") + Optional(modifier)("modifier") + id("ruleName") + Optional("!") + Optional(ARG_ACTION("arg")) + Optional(Suppress('returns') + ARG_ACTION("rt")) + Optional(throwsSpec) + Optional(optionsSpec) + Optional(ruleScopeSpec) + ZeroOrMore(ruleAction)
rule = Group(ruleHeading + Suppress(':') + altList + Suppress(';') + Optional(exceptionGroup))("rule")
grammarDef = grammarHeading + Group(OneOrMore(rule))("rules")
def grammar():
return grammarDef
def __antlrAlternativesConverter(pyparsingRules, antlrBlock):
rule = None
if hasattr(antlrBlock, 'alternatives') and antlrBlock.alternatives != '' and len(antlrBlock.alternatives) > 0:
alternatives = []
alternatives.append(__antlrAlternativeConverter(pyparsingRules, antlrBlock.a1))
for alternative in antlrBlock.alternatives:
alternatives.append(__antlrAlternativeConverter(pyparsingRules, alternative))
rule = MatchFirst(alternatives)("anonymous_or")
elif hasattr(antlrBlock, 'a1') and antlrBlock.a1 != '':
realNum = Regex(r"[+-]?(\d+\.\d*|\.\d+)([eE][+-]?\d+)?").addParseAction(unquote)
intNum = Regex(r"[+-]?\d+([eE]\+?\d+)?").addParseAction(unquote)
# STRINGS, NUMBERS, VARIABLES
sqlString = Regex(r"\'(\'\'|\\.|[^'])*\'").addParseAction(to_string)
identString = Regex(r'\"(\"\"|\\.|[^"])*\"').addParseAction(unquote)
mysqlidentString = Regex(r'\`(\`\`|\\.|[^`])*\`').addParseAction(unquote)
ident = Combine(~RESERVED + (delimitedList(Literal("*") | Word(alphas + "_", alphanums + "_$") | identString | mysqlidentString, delim=".", combine=True))).setName("identifier")
# EXPRESSIONS
expr = Forward()
# CASE
case = (
CASE +
Group(ZeroOrMore((WHEN + expr("when") + THEN + expr("then")).addParseAction(to_when_call)))("case") +
Optional(ELSE + expr("else")) +
END
).addParseAction(to_case_call)
selectStmt = Forward()
compound = (
(Keyword("not", caseless=True)("op").setDebugActions(*debug) + expr("params")).addParseAction(to_json_call) |
(Keyword("distinct", caseless=True)("op").setDebugActions(*debug) + expr("params")).addParseAction(to_json_call) |
Keyword("null", caseless=True).setName("null").setDebugActions(*debug) |
case |
(Literal("(").setDebugActions(*debug).suppress() + selectStmt + Literal(")").suppress()) |
(Literal("(").setDebugActions(*debug).suppress() + Group(delimitedList(expr)) + Literal(")").suppress()) |
realNum.setName("float").setDebugActions(*debug) |
intNum.setName("int").setDebugActions(*debug) |
(Literal("-")("op").setDebugActions(*debug) + expr("params")).addParseAction(to_json_call) |
sqlString.setName("string").setDebugActions(*debug) |
return (LPAREN + expr + RPAREN) | (LCURLY + expr + RCURLY)
# Define parser components for strings (the hard bit)
chars_no_curly = Regex(r"[^{}]+")
chars_no_curly.leaveWhitespace()
chars_no_quotecurly = Regex(r'[^"{}]+')
chars_no_quotecurly.leaveWhitespace()
# Curly string is some stuff without curlies, or nested curly sequences
curly_string = Forward()
curly_item = Group(curly_string) | chars_no_curly
curly_string << LCURLY + ZeroOrMore(curly_item) + RCURLY
# quoted string is either just stuff within quotes, or stuff within quotes, within
# which there is nested curliness
quoted_item = Group(curly_string) | chars_no_quotecurly
quoted_string = QUOTE + ZeroOrMore(quoted_item) + QUOTE
# Numbers can just be numbers. Only integers though.
number = Regex('[0-9]+')
# Basis characters (by exclusion) for variable / field names. The following
# list of characters is from the btparse documentation
any_name = Regex('[^\s"#%\'(),={}]+')
# btparse says, and the test bibs show by experiment, that macro and field names
# cannot start with a digit. In fact entry type names cannot start with a digit
# either (see tests/bibs). Cite keys can start with a digit
not_digname = Regex('[^\d\s"#%\'(),={}][^\s"#%\'(),={}]*')
# Comment comments out to end of line
comment = (AT + CaselessLiteral('comment') +
Regex("[\s{(].*").leaveWhitespace())
communicationList = Group(implementsList & requiresList & subscribesList & publishesList).setResultsName("communications")
communications = COMMUNICATIONS.suppress() - OBRACE + communicationList + CBRACE + SEMI
# Language
language_options = (CPP | CPP11 | PYTHON).setResultsName('language')
language = LANGUAGE.suppress() - language_options - SEMI
# Qtversion
qtVersion = Group(Optional(USEQt.suppress() - (QT4|QT5).setResultsName('useQt') + SEMI))
# InnerModelViewer
innermodelviewer = Group(Optional(INNERMODELVIEWER.suppress() - (TRUE|FALSE) + SEMI))('innermodelviewer')
# GUI
gui_options = QWIDGET|QMAINWINDOW|QDIALOG
gui = Group(Optional(GUI.suppress() - QT + OPAR - gui_options('gui_options') - CPAR + SEMI ))
# additional options
options = Group(Optional(OPTIONS.suppress() - identifier + ZeroOrMore(Suppress(Word(',')) + identifier) + SEMI))
statemachine = Group(Optional(STATEMACHINE.suppress() - QUOTE + CharsNotIn("\";").setResultsName('machine_path') + QUOTE + SEMI))
# Component definition
componentContents = Group(communications - language + Optional(gui('gui')) + Optional(options('options')) + Optional(qtVersion) + Optional(innermodelviewer) + Optional(statemachine('statemachine'))).setResultsName("content")
component = Group(COMPONENT.suppress() - identifier("name") + OBRACE + componentContents + CBRACE + SEMI).setResultsName("component")
CDSL = idslImports - component
return CDSL