def customFields(): return ( ZeroOrMore(RegExMatch(r'[\w]+')), # name ZeroOrMore('='), ZeroOrMore(RegExMatch(r'[\w]+')), # type ZeroOrMore(RegExMatch(r';.*')) # comment )
def defHeader(): return ( RegExMatch(r'[\w\d]+'), # name "{", Optional('//', RegExMatch(r'.*?(#|{})'.format(lineSep))), # comment Optional(RegExMatch(r'#?jadn_opts:{.*}+')), # jadn options OneOrMore(endLine))
def defHeader(): return ( # matches name: "(NAME) = { ... }" ZeroOrMore(RegExMatch(r'^[\w]+')), # matches line up to jadn_opts ZeroOrMore(RegExMatch(r'.*?(#|{})')), # matches jadn_opts ZeroOrMore(RegExMatch(r'#?jadn_opts:{.*}+')), OneOrMore(endLine))
def re_match_SA(parser, node, children): to_match = children[0] regex = RegExMatch(to_match, ignore_case=parser.metamodel.ignore_case) try: regex.compile() except Exception as e: line, col = parser.pos_to_linecol(node[1].position) raise TextXSyntaxError("{} at {}".format(text(e), text((line, col))), line, col) return regex
def visit_re_match(self, node, children): to_match = node.extra_info.group(1) # print("**** visit_re_match, to_match == '{}'".format(to_match)) regex = RegExMatch(to_match, ignore_case=self.metamodel.ignore_case) try: regex.compile() except Exception as e: line, col = self.grammar_parser.pos_to_linecol(node[1].position) raise TextXSyntaxError(text(e), line, col) return regex
def re_match_SA(parser, node, children): to_match = children[0] regex = RegExMatch(to_match, ignore_case=parser.metamodel.ignore_case) try: regex.compile() except Exception as e: line, col = parser.pos_to_linecol(node[1].position) raise TextXSyntaxError( "{} at {}" .format(text(e), text((line, col))), line, col) return regex
def defHeader(): return ( # match - word or digit character one or more times RegExMatch(r'[\w\d]+'), # name "{", # match - Any character (except line terminator) until # or line terminator Optional('//', RegExMatch(r'.*?(#|{})'.format(lineSep))), # comment # match - Capture jadn_opts within comment, last } is matched one or more times Optional(RegExMatch(r'#?jadn_opts:{.*}+')), # jadn options OneOrMore(endLine))
def enumField(): return ( RegExMatch(r'\w[\w\d]*?\s'), # name '=', number, # field number ';', Optional( '//', RegExMatch(r'.*?(#|{})'.format(lineSep)), # comment Optional(RegExMatch(r'jadn_opts:{.*}+')) # jadn options ), OneOrMore(endLine))
def defField(): return ( number, # field number ':', RegExMatch(r'\w[\w\d\.]*?\s'), # option RegExMatch(r'\w[\w\d\.]*?\s|list\<([\w\d]+)\>*?\s'), # type RegExMatch(r'(\w[\w\d]*?);'), # name Optional( '//', RegExMatch(r'.*?(#|{})'.format(lineSep)), # comment Optional(RegExMatch(r'jadn_opts:{.*}+')) # jadn options ), OneOrMore(endLine))
def str_match_SA(parser, node, children): to_match = children[0] # Support for autokwd metamodel param. if parser.metamodel.autokwd: match = parser.keyword_regex.match(to_match) if match and match.span() == (0, len(to_match)): regex_match = RegExMatch(r'{}\b'.format(to_match), ignore_case=parser.metamodel.ignore_case, str_repr=to_match) regex_match.compile() return regex_match return StrMatch(to_match, ignore_case=parser.metamodel.ignore_case)
def visit_re_match(self, node, children): try: to_match = children[0] except: to_match = '' regex = RegExMatch(to_match, ignore_case=self.metamodel.ignore_case) try: regex.compile() except Exception as e: line, col = self.grammar_parser.pos_to_linecol(node[1].position) raise TextXSyntaxError( "{} at {}".format(text(e), text((line, col))), line, col) return regex
def defField(): return ( Optional('?'), # matches (TYPE) : ... RegExMatch(r'\s*[\w]+'), # type # matches type : (NAME), ... RegExMatch(r':\s*[\w]+\s*,*'), # name Optional('//'), Optional( ';', RegExMatch(r'.*?(#|{})'.format(lineSep)), # comment Optional(RegExMatch(r'jadn_opts:{.*}+')) # jadn options ), OneOrMore(endLine))
def defHeader(): return ( # match - word or digit character one or more times RegExMatch(r'[\w\d]+'), # name "{", Optional( '//', # match - any character (except line terminator) until # or line terminator RegExMatch(r'.*?(#|{})'.format(lineSep)) # comment ), # match - (#)jadn_opts:{JADN OPTS} # last } is matched one or more times Optional(RegExMatch(r'#?jadn_opts:{.*}+')), # jadn options OneOrMore(endLine))
def enumField(): return ( # matches (ENUM) = ... RegExMatch(r'[\w]+'), # enum name Optional('/='), Optional('='), # matches ... = (NAME) RegExMatch(r'\"[\w]+\"'), # item name ';', Optional( RegExMatch(r'.*?(#|{})'.format(lineSep)), # comment Optional(RegExMatch(r'jadn_opts:{.*}+')) # jadn options ), OneOrMore(endLine))
def enumField(): return ( # match - Any group until space RegExMatch(r'\w[\w\d]*?\s'), # name '=', number, # field number ';', Optional( '//', # match - Any character (except line terminator) until # or line terminator RegExMatch(r'.*?(#|{})'.format(lineSep)), # comment # match - Capture jadn_opts within comment, last } is matched one or more times Optional(RegExMatch(r'jadn_opts:{.*}+')) # jadn options ), OneOrMore(endLine))
def visit_re_match(self, node, children): try: to_match = children[0] except IndexError: to_match = '' regex = RegExMatch(to_match, ignore_case=self.metamodel.ignore_case) try: regex.compile() except Exception as e: line, col = self.grammar_parser.pos_to_linecol(node[1].position) raise TextXSyntaxError( "{} at {}" .format(text(e), text((line, col))), line, col) return regex
def enumField(): return ( # match - any word character followed by one or more word or digit characters until a space RegExMatch(r'\w[\w\d]*?\s'), # name '=', number, # field number ';', Optional( '//', # match - any character (except line terminator) until # or line terminator RegExMatch(r'.*?(#|{})'.format(lineSep)), # comment # match - (#)jadn_opts:{JADN OPTS} # last } is matched one or more times Optional(RegExMatch(r'jadn_opts:{.*}+')) # jadn options ), OneOrMore(endLine))
def wrappedDef(): return ( 'struct', # match - Any group until a non word/digit RegExMatch(r'[\w\d]+'), '{', Optional(repeatedDef), '}')
def visit_str_match(self, node, children): try: to_match = children[0][1:-1] except IndexError: to_match = '' # Support for autokwd metamodel param. if self.metamodel.autokwd: match = self.keyword_regex.match(to_match) if match and match.span() == (0, len(to_match)): regex_match = RegExMatch( r'{}\b'.format(to_match), ignore_case=self.metamodel.ignore_case, str_repr=to_match) regex_match.compile() return regex_match return StrMatch(to_match, ignore_case=self.metamodel.ignore_case)
def wrappedDef(): return ( 'message', # match - any word or digit character one or more word or digit characters until a non word/digit character RegExMatch(r'[\w\d]+'), '{', OrderedChoice(Optional(oneofDef), Optional(repeatedDef)), '}')
def defField(): return ( number, # field number ':', # match - Any group until space RegExMatch(r'\w[\w\d\.]*?\s'), # option # match - Any group until space OR any group enclosed within 'list<>' RegExMatch(r'\w[\w\d\.]*?\s|list\<([\w\d]+)\>*?\s'), # type # match - Any group until space RegExMatch(r'(\w[\w\d]*?);'), # name Optional( '//', # match - Any character (except line terminator) until # or line terminator RegExMatch(r'.*?(#|{})'.format(lineSep)), # comment # match - Capture jadn_opts within comment, last } is matched one or more times Optional(RegExMatch(r'jadn_opts:{.*}+')) # jadn options ), OneOrMore(endLine))
def check_parse(self, fcn, rule_f, rule, words, text, expect_list): # Unused: fcn, rule_f newline = common.newline() catchall = RegExMatch(r'.*', rule_name='catch_all') elements = [rule, words, catchall, newline] def grammar(): return Sequence((OneOrMore(OrderedChoice(elements)), EOF), rule_name="grammar") self.verify_grammar(grammar, text, expect_list, skipws=False)
def ReservedWord(): '''Matches identifiers that aren't allowed as variable names.''' command_word_parsers = [] for cmd_type in Command(): cmd_parser = cmd_type() if isinstance(cmd_parser, tuple): command_word_parsers.append(cmd_parser[0]) else: command_word_parsers.append(cmd_parser) return ([ # Starts with a roll expression RollExpr, # Matches a command word exactly (command_word_parsers, [RegExMatch('[^A-Za-z0-9_]'), EOF]), ])
def numeric(): return RegExMatch(r'[0-9]+(\.[0-9]*)?')
def roman(): return RegExMatch(r'(I[VX]|VI{0,3}|I{1,3})([Aa]?[Bb]?)')
def grammar(): return ZeroOrMore("a", sep=RegExMatch(",?")), EOF
def word(): return RegExMatch(r"\w+")
def grammar(): return OneOrMore("a", sep=RegExMatch(",?")), "b"
def endLine(): # match - Line terminator return RegExMatch(r'({})?'.format(lineSep))
def commentLine(): # match - Any character, non line terminator return '//', RegExMatch(r'.*')
def commentBlock(): # match - Any group of characters that are commented (/* & * & */) return RegExMatch(r'\/\*(.|{})*?\*\/'.format(lineSep)),
def number(): # match - Numbers return RegExMatch(r'\d*\.\d*|\d+')