Permalink
Browse files

Work on bootstrap.

  • Loading branch information...
1 parent 55de2e3 commit 64fdc1d35069b4e7cc670b408094e1eb099989f9 @rampelstinskin rampelstinskin committed Apr 20, 2012
Showing with 33 additions and 76 deletions.
  1. +12 −1 Test/CalcGrammar.n2
  2. +21 −75 Test/Main.n
View
@@ -4,7 +4,7 @@ syntax module CalcGrammar
{
option EmitDebugSources = true;
- using IncParser;
+ using IncGrammar;
using NumParser;
token any = ['\u0000'..'\uFFFF'];
@@ -34,4 +34,15 @@ syntax module CalcGrammar
[Ast(l, op, r)] rule pow is expr = expr : 31 '^'s expr : 30;
[Ast(cond, q, l, colon, r)] rule cond is expr = expr : 301 '?'s expr ':'s expr;
+
+ [Ast(l, CoalescingOp, r)] rule coalescing is expr = expr : 401 "??"s expr : 400;
+}
+
+syntax module IncGrammar
+{
+ using cp = CalcGrammar;
+
+ [Ast(op, expr)] rule plus is cp.expr = '+'cp.s cp.expr : 100;
+ [Ast(op, expr)] rule prefixInc is cp.expr = "++"cp.s cp.expr : 200;
+ [Ast(expr, op)] rule postfixInc is cp.expr = cp.expr : 200 "++"cp.s;
}
View
@@ -10,60 +10,6 @@ using SCG = System.Collections.Generic;
using System.Console;
using System.Linq;
-[ParserGrammar(Options = EmitDebugSources,
- parsergrammar
- {
- using IncParser;
- using NumParser;
-
- any = ['\u0000'..'\uFFFF'];
- s : void = ' '*;
-
- [StartRule, Ast(expr)]
- start : Ast = s expr !any;
-
- [StartRule, Ast()]
- expr : Ast;
-
- [Ast(l, expr, r)] rounds is expr = '('s expr ')'s;
- [Ast(l, expr, r)] seq is expr = '{'s expr* '}'s;
-
- [Ast(num)] num is expr = number s;
-
- [Ast(op, expr)] neg is expr = '-'s expr : 100;
-
- [Ast(op, expr)] prefixDec is expr = "--"s expr : 200;
- [Ast(expr, op)] postfixDec is expr = expr : 200 "--"s;
-
- [Ast(l, op, r)] add is expr = expr : 10 '+'s expr : 10;
- [Ast(l, op, r)] sub is expr = expr : 10 '-'s expr : 10;
- [Ast(l, op, r)] mul is expr = expr : 20 '*'s expr : 20;
- [Ast(l, op, r)] div is expr = expr : 20 '/'s expr : 20;
- [Ast(l, op, r)] mod is expr = expr : 20 '%'s expr : 20;
- [Ast(l, op, r)] pow is expr = expr : 31 '^'s expr : 30;
-
- [Ast(cond, q, l, colon, r)] cond is expr = expr : 301 '?'s expr ':'s expr;
- [Ast(l, CoalescingOp, r)] coalescing is expr = expr : 401 "??"s expr : 400;
- }
-)]
-public abstract class CalcParser
-{}
-
-[ParserGrammar(Options = EmitDebugSources,
- parsergrammar
- {
- //using CalcParser;
- using cp = CalcParser;
-
- [Ast(op, expr)] plus is cp.expr = '+'cp.s cp.expr : 100;
- [Ast(op, expr)] prefixInc is cp.expr = "++"cp.s cp.expr : 200;
- [Ast(expr, op)] postfixInc is cp.expr = cp.expr : 200 "++"cp.s;
- }
-)]
-public abstract class IncParser
-{
-}
-
[ParserGrammar(Options = EmitDebugSources,
parsergrammar
{
@@ -138,7 +84,7 @@ module Program
{
def print(text)
{
- def parser = CalcParser.GrammarImpl();
+ def parser = CalcGrammar.GrammarImpl();
match (parser.ParseStart(text))
{
| None =>
@@ -148,29 +94,29 @@ module Program
foreach (token in tokens)
WriteLine($" $line:$pos expected \"$(token.Name)\" in rule $(token.Rule.Grammar.Name).$(token.Rule.Name)");
- | Some(CalcParser.Start.Ast(ast)) =>
- def printAst(ast : CalcParser.Expr)
+ | Some(CalcGrammar.Start.Ast(ast)) =>
+ def printAst(ast : CalcGrammar.Expr)
{
match (ast)
{
- | CalcParser.Rounds.Ast(_, ast, _) => $"( $(printAst(ast)) )"
- | CalcParser.Seq.Ast(_, asts, _) => $<#{ ..$(asts; " ; "; printAst(_)) }#>
- | CalcParser.Num.Ast(num) => $"$(num.GetText((num :> NumParser.Number.Ast).number))"
- | CalcParser.Neg.Ast(_, v) => $"[ - $(printAst(v)) ]"
- | CalcParser.PrefixDec.Ast(_, v) => $"[ -- $(printAst(v)) ]"
- | CalcParser.PostfixDec.Ast(v, _) => $"[ $(printAst(v)) -- ]"
- | CalcParser.Add.Ast(l, _, r) => $"[ $(printAst(l)) + $(printAst(r)) ]"
- | CalcParser.Sub.Ast(l, _, r) => $"[ $(printAst(l)) - $(printAst(r)) ]"
- | CalcParser.Mul.Ast(l, _, r) => $"[ $(printAst(l)) * $(printAst(r)) ]"
- | CalcParser.Div.Ast(l, _, r) => $"[ $(printAst(l)) / $(printAst(r)) ]"
- | CalcParser.Mod.Ast(l, _, r) => $"[ $(printAst(l)) % $(printAst(r)) ]"
- | CalcParser.Pow.Ast(l, _, r) => $"[ $(printAst(l)) ^ $(printAst(r)) ]"
- | CalcParser.Coalescing.Ast(l, _, r) => $"[ $(printAst(l)) ?? $(printAst(r)) ]"
- | CalcParser.Cond.Ast(cond, _, l, _, r) => $"[ $(printAst(cond)) ? $(printAst(l)) : $(printAst(r)) ]"
-
- | IncParser.Plus.Ast(_, v) => $"[ + $(printAst(v)) ]"
- | IncParser.PrefixInc.Ast(_, v) => $"[ ++ $(printAst(v)) ]"
- | IncParser.PostfixInc.Ast(v, _) => $"[ $(printAst(v)) ++ ]"
+ | CalcGrammar.Rounds.Ast(_, ast, _) => $"( $(printAst(ast)) )"
+ | CalcGrammar.Seq.Ast(_, asts, _) => $<#{ ..$(asts; " ; "; printAst(_)) }#>
+ | CalcGrammar.Num.Ast(num) => $"$(num.GetText((num :> NumParser.Number.Ast).number))"
+ | CalcGrammar.Neg.Ast(_, v) => $"[ - $(printAst(v)) ]"
+ | CalcGrammar.PrefixDec.Ast(_, v) => $"[ -- $(printAst(v)) ]"
+ | CalcGrammar.PostfixDec.Ast(v, _) => $"[ $(printAst(v)) -- ]"
+ | CalcGrammar.Add.Ast(l, _, r) => $"[ $(printAst(l)) + $(printAst(r)) ]"
+ | CalcGrammar.Sub.Ast(l, _, r) => $"[ $(printAst(l)) - $(printAst(r)) ]"
+ | CalcGrammar.Mul.Ast(l, _, r) => $"[ $(printAst(l)) * $(printAst(r)) ]"
+ | CalcGrammar.Div.Ast(l, _, r) => $"[ $(printAst(l)) / $(printAst(r)) ]"
+ | CalcGrammar.Mod.Ast(l, _, r) => $"[ $(printAst(l)) % $(printAst(r)) ]"
+ | CalcGrammar.Pow.Ast(l, _, r) => $"[ $(printAst(l)) ^ $(printAst(r)) ]"
+ | CalcGrammar.Coalescing.Ast(l, _, r) => $"[ $(printAst(l)) ?? $(printAst(r)) ]"
+ | CalcGrammar.Cond.Ast(cond, _, l, _, r) => $"[ $(printAst(cond)) ? $(printAst(l)) : $(printAst(r)) ]"
+
+ | IncGrammar.Plus.Ast(_, v) => $"[ + $(printAst(v)) ]"
+ | IncGrammar.PrefixInc.Ast(_, v) => $"[ ++ $(printAst(v)) ]"
+ | IncGrammar.PostfixInc.Ast(v, _) => $"[ $(printAst(v)) ++ ]"
| _ => assert(false);
}
}

0 comments on commit 64fdc1d

Please sign in to comment.