diff --git a/sample.ret b/sample.ret index 7f3a426..325e08a 100644 --- a/sample.ret +++ b/sample.ret @@ -19,4 +19,4 @@ FIZZ_BUZZ = [ ~ '\0' ] -':::' => FIZZ_BUZZ \ No newline at end of file +':::' => FIZZ_BUZZ diff --git a/sub.py b/sub.py index f81af21..b3d64fa 100644 --- a/sub.py +++ b/sub.py @@ -10,14 +10,15 @@ suite: prod* ?prod: _named_prod | _direct_prod _named_prod : alias -_direct_prod : _compound_prod | _rule_prod | _literal_prod | _abstract_prod -_abstract_prod : output +_direct_prod : _compound_prod | _rule_prod | _literal_prod | _io_prod +_io_prod : output | input _compound_prod : cont | sing _rule_prod : full | part _literal_prod : lit | ref alias : name "=" prod // production alias output: "~" prod // output production +input: ":::" // input production cont : "{" suite "}" // continual production sing : "[" suite "]" // singular production @@ -58,8 +59,8 @@ class Context: return diff def expand(self, fmt): - self.string = self.match.expand(fmt) self.match = ALL_PAT.match(self.string) + self.string = self.match.expand(fmt) def __str__(self): return f'Ctx[{self.string!r} {self.match[0]!r} {self.match.groups()}]' @@ -114,6 +115,31 @@ class Partial: return f'Part[{self.reg} ::= {self.rhs}]' +class Input: + def apply(self, ctx): + ctx_ = ctx.enter() + ctx_.string = input() + ctx_.match = ALL_PAT.match(ctx_.string) + return ctx.exit(ctx_) + + def __str__(self): + return 'Input' + + +class Output: + def __init__(self, prod): + self.prod = prod + + def apply(self, ctx): + _ctx = ctx.enter() + self.prod.apply(_ctx) + print(_ctx.string) + return False + + def __str__(self): + return f'Output[{self.prod}]' + + class Suite: def __init__(self, prods, ctx=None): if ctx is None: @@ -191,6 +217,13 @@ class ProductionTransformer(Transformer): match = REGEX_PAT.match(reg) return re.compile(match[3]) + def input(self, prods): + return Input() + + def output(self, prods): + prod, = prods + return Output(prod) + def full(self, prods): lhs, rhs = prods return Full(lhs, rhs) @@ -224,6 +257,7 @@ def main(): tform = ProductionTransformer() pgm = tform.transform(tree) print(pgm) + print() print(pgm.run()) diff --git a/testing.ret b/testing.ret index b21bc22..4335af4 100644 --- a/testing.ret +++ b/testing.ret @@ -1 +1,7 @@ -'hello there' => /(.*)\s+(.*)/ ::= '\g<0> \2 \1' \ No newline at end of file +::: => { + ~ "\g<0>" + /[aeiou]/ ::= '_' + /[^aeiou_\W]/ ::= ':' + + /_:/ ::= [~ "a" ":"] +} \ No newline at end of file