# Copyright 2005 Kevin Reid, under the terms of the MIT X license # found at http://www.opensource.org/licenses/mit-license.html ................ pragma.enable("easy-return") pragma.disable("explicit-result-guard") pragma.enable("verb-curry") def mapStream := def makeTermParser implements DeepFrozen { to run(builder) { # XXX move outside once it is DeepFrozen def lalr1__quasiParser := .getLalr1__quasiParser() def b := builder def leafDataAction(data) { return b.leafData(data, null) } # XXX SourceSpan def lparser := lalr1` "TermL" := start. start := term => ${b.start}. term := functor => ${b.term}. term := functor lpar argList rpar => ${def _(f,_,a,_) { return \ b.term(f,a)}}. term := functor lbr argList rbr => ${def _(f,_,a,_) { return \ b.term(f,b.bag(a))}}. term := lbr argList rbr => ${def _(_,a,_) { return \ b.bag(a)}}. term := ${"["} argList ${"]"} => ${def _(_,a,_) { return \ b.tuple(a)}}. term := functor ${":"} term => ${def _(f,_,a) { return \ b.attr(f,a)}}. argList := => ${b.empty}. argList := term => $__identityFunc. argList := term ${","} argList => ${def _(l,_,r) { return \ b.seq(l,r)}}. functor := tag => $__identityFunc. functor := literal => $__identityFunc. functor := functorHole => $__identityFunc. functor := tag functorHole => ${b.taggedHole}. functorHole := ${"Quasi$"} => ${b.valueHole}. functorHole := ${"Quasi@"} => ${b.patternHole}. tag := ${"Tag"} => ${b.tag}. literal := ${"Integer"} => $leafDataAction. literal := ${"Float64"} => $leafDataAction. literal := ${"Char"} => $leafDataAction. literal := ${"String"} => $leafDataAction. lbr := ${"{"}. rbr := ${"}"}. lpar := ${"("}. rpar := ${")"}. ` # lbr and rbr are necessary only due to a current limitation in lalr1__quasiParser. def termParser { /** Parse the given stream of term-language text to a quasi-term tree. */ to parseText(textStream) { return termParser.parse((builder.doesQuasis())(textStream)) } /** Parse the given stream of term-language tokens to a quasi-term tree. */ to parse(lexStream) { return lparser.parse(mapStream(any, lexStream, def tf(t) { return switch (t) { match [x] { [x, null] } match [x,y] { [x, y] } match [x] + y { [x, y] } } })) } } return termParser } }