internal StandardGrammar() { this._isReadOnly = false; var newline = Combinator.Choice( Chars.Sequence("\r\n"), Chars.OneOf('\r', '\n', '\x85', '\u2028', '\u2029') .Select(EnumerableEx.Return) ).Select(_ => Environment.NewLine); var punctuation = Chars.OneOf('"', '\'', '(', ')', ',', '.', ':', ';', '[', ']', '`', '{', '}'); Parser <Char, YacqExpression> expressionRef = null; var expression = new Lazy <Parser <Char, YacqExpression> >( () => stream => expressionRef(stream) ); this.Add("yacq", "expression", g => expression.Value); // Comments { this.Add("comment", "eol", g => Prims.Pipe( ';'.Satisfy(), newline.Not().Right(Chars.Any()).Many(), newline.Ignore().Or(Chars.Eof()), (p, r, s) => (YacqExpression)YacqExpression.Ignore() )); Parser <Char, YacqExpression> blockCommentRef = null; Parser <Char, YacqExpression> blockCommentRestRef = null; var blockComment = new Lazy <Parser <Char, YacqExpression> >( () => stream => blockCommentRef(stream) ); var blockCommentRest = new Lazy <Parser <Char, YacqExpression> >( () => stream => blockCommentRestRef(stream) ); var blockCommentPrefix = Chars.Sequence("#|"); var blockCommentSuffix = Chars.Sequence("|#"); blockCommentRef = blockCommentPrefix .Right(blockCommentRest.Value.Many()) .Left(blockCommentSuffix) .Select(_ => (YacqExpression)YacqExpression.Ignore()); blockCommentRestRef = blockCommentPrefix.Not() .Right(blockCommentSuffix.Not()) .Right(Chars.Any()) .Select(_ => (YacqExpression)YacqExpression.Ignore()) .Or(blockComment.Value); this.Add("comment", "block", g => blockComment.Value); this.Add("comment", "expression", g => Prims.Pipe( Chars.Sequence("#;"), g["yacq", "expression"], (p, r) => (YacqExpression)YacqExpression.Ignore() )); this.Add("yacq", "comment", g => Combinator.Choice(g["comment"])); } this.Add("yacq", "ignore", g => Combinator.Choice( this.Get["yacq", "comment"].Ignore(), Chars.Space().Ignore(), newline.Ignore() ).Many().Select(_ => (YacqExpression)YacqExpression.Ignore())); // Texts this.Add("term", "text", g => SetPosition( Chars.OneOf('\'', '\"', '`') .SelectMany(q => q.Satisfy() .Not() .Right('\\'.Satisfy() .Right(q.Satisfy()) .Or(Chars.Any()) ) .Many() .Left(q.Satisfy()) .Select(cs => cs.StartWith(q).EndWith(q)) ) .Select(cs => (YacqExpression)YacqExpression.Text(new String(cs.ToArray()))) )); // Numbers { var numberPrefix = Chars.OneOf('+', '-'); var numberSuffix = Combinator.Choice( Chars.Sequence("ul"), Chars.Sequence("UL"), Chars.OneOf('D', 'F', 'L', 'M', 'U', 'd', 'f', 'l', 'm', 'u') .Select(EnumerableEx.Return) ); var digit = '_'.Satisfy().Many().Right(Chars.Digit()); var hexPrefix = Chars.Sequence("0x"); var hex = '_'.Satisfy().Many().Right(Chars.Hex()); var octPrefix = Chars.Sequence("0o"); var oct = '_'.Satisfy().Many().Right(Chars.Oct()); var binPrefix = Chars.Sequence("0b"); var bin = '_'.Satisfy().Many().Right(Chars.OneOf('0', '1')); var fraction = Prims.Pipe( '.'.Satisfy(), digit.Many(1), (d, ds) => ds.StartWith(d) ); var exponent = Prims.Pipe( Chars.OneOf('E', 'e'), Chars.OneOf('+', '-').Maybe(), digit.Many(1), (e, s, ds) => ds .If(_ => s.Exists(), _ => _.StartWith(s.Perform())) .StartWith(e) ); this.Add("term", "number", g => Combinator.Choice( SetPosition(Prims.Pipe( binPrefix, bin.Many(1), numberSuffix.Maybe(), (p, n, s) => (YacqExpression)YacqExpression.Number( new String(p.Concat(n).If( _ => s.Exists(), cs => cs.Concat(s.Perform()) ).ToArray()) ) )), SetPosition(Prims.Pipe( octPrefix, oct.Many(1), numberSuffix.Maybe(), (p, n, s) => (YacqExpression)YacqExpression.Number( new String(p.Concat(n).If( _ => s.Exists(), cs => cs.Concat(s.Perform()) ).ToArray()) ) )), SetPosition(Prims.Pipe( hexPrefix, hex.Many(1), numberSuffix.Maybe(), (p, n, s) => (YacqExpression)YacqExpression.Number( new String(p.Concat(n).If( _ => s.Exists(), cs => cs.Concat(s.Perform()) ).ToArray()) ) )), SetPosition( numberPrefix.Maybe().SelectMany(p => digit.Many(1).SelectMany(i => fraction.Maybe().SelectMany(f => exponent.Maybe().SelectMany(e => numberSuffix.Maybe().Select(s => (YacqExpression)YacqExpression.Number(new String(EnumerableEx.Concat( i.If(_ => p.Exists(), _ => _.StartWith(p.Perform())), f.Otherwise(Enumerable.Empty <Char>), e.Otherwise(Enumerable.Empty <Char>), s.Otherwise(Enumerable.Empty <Char>) ).ToArray())) ) ) ) ) ) ) )); } // Lists this.Add("term", "list", g => SetPosition( g["yacq", "expression"] .Between(g["yacq", "ignore"], g["yacq", "ignore"]) .Many() .Between('('.Satisfy(), ')'.Satisfy()) .Select(es => (YacqExpression)YacqExpression.List(es)) )); // Vectors this.Add("term", "vector", g => SetPosition( g["yacq", "expression"] .Between(g["yacq", "ignore"], g["yacq", "ignore"]) .Many() .Between('['.Satisfy(), ']'.Satisfy()) .Select(es => (YacqExpression)YacqExpression.Vector(es)) )); // Lambda Lists this.Add("term", "lambdaList", g => SetPosition( g["yacq", "expression"] .Between(g["yacq", "ignore"], g["yacq", "ignore"]) .Many() .Between('{'.Satisfy(), '}'.Satisfy()) .Select(es => (YacqExpression)YacqExpression.LambdaList(es)) )); // Quotes this.Add("term", "quote", g => SetPosition( Prims.Pipe( Chars.Sequence("#'"), g["yacq", "expression"], (p, e) => (YacqExpression)YacqExpression.List(YacqExpression.Identifier("quote"), e) ) )); // Quasiquotes this.Add("term", "quasiquote", g => SetPosition( Prims.Pipe( Chars.Sequence("#`"), g["yacq", "expression"], (p, e) => (YacqExpression)YacqExpression.List(YacqExpression.Identifier("quasiquote"), e) ) )); // Unquote-Splicings this.Add("term", "unquoteSplicing", g => SetPosition( Prims.Pipe( Chars.Sequence("#,@"), g["yacq", "expression"], (p, e) => (YacqExpression)YacqExpression.List(YacqExpression.Identifier("unquote-splicing"), e) ) )); // Unquotes this.Add("term", "unquote", g => SetPosition( Prims.Pipe( Chars.Sequence("#,"), g["yacq", "expression"], (p, e) => (YacqExpression)YacqExpression.List(YacqExpression.Identifier("unquote"), e) ) )); // Identifiers this.Add("term", "identifier", g => Combinator.Choice( SetPosition('.'.Satisfy() .Many(1) .Select(cs => (YacqExpression)YacqExpression.Identifier(new String(cs.ToArray()))) ), SetPosition(':'.Satisfy() .Many(1) .Select(cs => (YacqExpression)YacqExpression.Identifier(new String(cs.ToArray()))) ), SetPosition(Chars.Digit() .Not() .Right(Chars.Space() .Or(punctuation) .Not() .Right(Chars.Any()) .Many(1) ) .Select(cs => (YacqExpression)YacqExpression.Identifier(new String(cs.ToArray()))) ) )); // Terms this.Add("yacq", "term", g => Combinator.Choice(g["term"]) .Between(g["yacq", "ignore"], g["yacq", "ignore"]) ); // Infix Dots this.Add("infix", "dot", g => Prims.Pipe( g["yacq", "term"], '.'.Satisfy() .Right(g["yacq", "term"]) .Many(), (h, t) => t.Aggregate(h, (l, r) => (YacqExpression)YacqExpression.List(YacqExpression.Identifier("."), l, r) ) )); // Infix Colons this.Add("infix", "colon", g => Prims.Pipe( g["infix", "dot"], ':'.Satisfy() .Right(g["infix", "dot"]) .Many(), (h, t) => t.Aggregate(h, (l, r) => (YacqExpression)YacqExpression.List(YacqExpression.Identifier(":"), l, r) ) )); expressionRef = this.Get["infix"].Last(); this.Set.Default = g => g["yacq", "expression"]; this._isReadOnly = true; }