From 19f2b52e840e291bf32cbb904f6aa2664ab0e27e Mon Sep 17 00:00:00 2001 From: Joel Bender Date: Wed, 18 Nov 2015 08:47:36 -0500 Subject: [PATCH] moved the context specification to the end of the statement --- sandbox/extended_tag_list.py | 64 +++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/sandbox/extended_tag_list.py b/sandbox/extended_tag_list.py index fb13af7..493f16c 100644 --- a/sandbox/extended_tag_list.py +++ b/sandbox/extended_tag_list.py @@ -7,21 +7,21 @@ ExtendedTagList An extended tag list adds a loads() function which takes a blob of text and parses it into a list of tags using the following mini language: - opening [tag] n - closing [tag] n - [ [context] n ] null - [ [context] n ] boolean (false | true) - [ [context] n ] unsigned [0-9]+ - [ [context] n ] integer [+-][0-9]+ - [ [context] n ] real [+-][0-9]+([.][0-9]+)? - [ [context] n ] double [+-][0-9]+([.][0-9]+)? - [ [context] n ] octet [string] OCTETSTR - [ [context] n ] [character] string ([0-9]+)? CHARSTR - [ [context] n ] bit [string] BITSTR - [ [context] n ] enumerated [0-9]+ - [ [context] n ] date DATESTR - [ [context] n ] time TIMESTR - [ [context] n ] object [identifier] OBJTYPE [,] OBJINST + opening [tag] [context] n + closing [tag] [context] n + null [ [context] n ] + boolean (false | true) [ [context] n ] + unsigned [0-9]+ [ [context] n ] + integer [+-][0-9]+ [ [context] n ] + real [+-][0-9]+([.][0-9]+)? [ [context] n ] + double [+-][0-9]+([.][0-9]+)? [ [context] n ] + octet [string] OCTETSTR [ [context] n ] + [character] string ([0-9]+)? CHARSTR [ [context] n ] + bit [string] BITSTR [ [context] n ] + enumerated [0-9]+ [ [context] n ] + date DATESTR [ [context] n ] + time TIMESTR [ [context] n ] + object [identifier] OBJTYPE [,] OBJINST [ [context] n ] Blank lines and everything after the comment '#' is ignored. @@ -48,26 +48,24 @@ from bacpypes.primitivedata import * _debug = 0 _log = ModuleLogger(globals()) -# parse the command line arguments -args = ArgumentParser(description=__doc__).parse_args() - - # globals statements = [] statement_pattern = r""" - ^ \s* # leading white space - ( (context\s+)? (?P[0-9]+) \s+ )? # optional context + ^ + \s* # leading white space (%s) # keyword - ([#].*)? $ # optional comment - """ + ( (\s+context)? \s+ (?P[0-9]+))? # optional context + ([#].*)? # optional comment + $""" statement_value_pattern = r""" - ^ \s* # leading white space - ( (context\s+)? (?P[0-9]+) \s+ )? # optional context - (%s) \s+ (?P%s) \s* # keyword and value - ([#].*)? $ # optional comment - """ + ^ + \s* # leading white space + (%s) \s+ (?P%s) # keyword and value + ( (\s+context)? \s+ (?P[0-9]+))? # optional context + \s* ([#].*)? # optional comment + $""" # # blank lines @@ -117,13 +115,13 @@ def statement(pattern): # statements # -@statement(r"opening(\s+tag)? [0-9]+") +@statement(r"opening(\s+tag)?(\s+context)? [0-9]+") def opening_tag_statement(value): if _debug: ExtendedTagList._debug("opening_tag_statement %r", value) return OpeningTag(int(value)) -@statement(r"closing(\s+tag)? [0-9]+") +@statement(r"closing(\s+tag)?(\s+context)? [0-9]+") def closing_tag_statement(value): if _debug: ExtendedTagList._debug("closing_tag_statement %r", value) @@ -273,6 +271,8 @@ class ExtendedTagList(TagList): # check for element already a tag if isinstance(element, Tag): tag = element + if context is not None: + raise RuntimeError("syntax error: %r" % (line,)) elif isinstance(element, Atomic): tag = Tag() @@ -310,6 +310,10 @@ class ExtendedTagList(TagList): def main(): if _debug: main._debug("main") + # parse the command line arguments + args = ArgumentParser(description=__doc__).parse_args() + if _debug: main._debug(" - args: %r", args) + # suck in the test content text = sys.stdin.read()