Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Work on syntax. No more support for old syntax.

  • Loading branch information...
commit 9b6425ced5ed88a99785e47afb0eccdfc9ae66c9 1 parent ff56358
rampelstinskin authored
39 Nemerle.Parser.Macro/GrammarParser/Constructor.n
... ... @@ -1,39 +0,0 @@
1   -using System;
2   -using Nemerle;
3   -using Nemerle.Collections;
4   -using Nemerle.Text;
5   -using Nemerle.Utility;
6   -using Nemerle.Compiler;
7   -using Nemerle.Compiler.Parsetree;
8   -using Nemerle.Compiler.Parsetree.PExpr;
9   -using Nemerle.Compiler.Token;
10   -
11   -namespace Nemerle.Parser
12   -{
13   - internal partial class GrammarParser
14   - {
15   - // Rule = RuleName ((':' Type)? '=' OrderedChoice)?
16   - // OrderedChoice = Sequence ('/' Sequence)*
17   - // Sequence = PredicateRule+
18   - // PredicateRule = ('!' / '&')? CardinalityRule
19   - // CardinalityRule = SimpleRule ('?' / '+' / '*')?
20   - // SimpleRule = FailureRecovery '(' OrderedChoice ')', RuleName '{' OrderedChoice '}' / RuleName / Range / Char / String / '(' OrderedChoice ')' / Empty
21   - // RuleName = Token.Identifier(name)
22   - // Char = Token.CharLiteral
23   - // String = Token.StringLiteral
24   - // Range = Token.SquareGroup(LooseGroup(CharLiteral, Operator(".."), CharLiteral))
25   - // Eq = Token.Operator("=")
26   - // Empty = Token.Identifier("Empty")
27   -
28   - private _debug : bool;
29   - private _typer : Typer;
30   - private _parser : MainParser;
31   -
32   - public this(typer : Typer, debug : bool)
33   - {
34   - _typer = typer;
35   - _parser = MainParser(typer.Env);
36   - _debug = debug;
37   - }
38   - }
39   -}
41 Nemerle.Parser.Macro/GrammarParser/ErrorReport.n
... ... @@ -1,41 +0,0 @@
1   -using System;
2   -using Nemerle;
3   -using Nemerle.Collections;
4   -using Nemerle.Text;
5   -using Nemerle.Utility;
6   -using Nemerle.Compiler;
7   -using Nemerle.Compiler.Parsetree;
8   -using Nemerle.Compiler.Parsetree.PExpr;
9   -using Nemerle.Compiler.Token;
10   -
11   -namespace Nemerle.Parser
12   -{
13   - public class CriticalUnexpectedException : Exception { }
14   -
15   - internal partial class GrammarParser
16   - {
17   - ReportCriticalUnexpected[T](token : Token, expected : string) : T
18   - {
19   - ReportUnexpected(token, expected);
20   - throw CriticalUnexpectedException()
21   - }
22   -
23   - ReportUnexpected(token : Token, expected : string) : void
24   - {
25   - assert2(!_debug);
26   - Message.Error(token.Location, $"expected «$expected» but found «$token» ($(token.GetType().Name))");
27   - }
28   -
29   - ReportUnexpected(expr : PExpr, expected : string) : void
30   - {
31   - assert2(!_debug);
32   - Message.Error(expr.Location, $"expected $expected but found $expr ($(expr.GetType().Name))");
33   - }
34   -
35   - ReportCriticalUnexpected[T](expr : PExpr, expected : string) : T
36   - {
37   - ReportUnexpected(expr, expected);
38   - throw CriticalUnexpectedException()
39   - }
40   - }
41   -}
119 Nemerle.Parser.Macro/GrammarParser/ParseAttribute.n
... ... @@ -1,119 +0,0 @@
1   -using System;
2   -using Nemerle;
3   -using Nemerle.Collections;
4   -using Nemerle.Text;
5   -using Nemerle.Utility;
6   -using Nemerle.Compiler;
7   -using Nemerle.Compiler.Parsetree;
8   -using Nemerle.Compiler.Parsetree.PExpr;
9   -using Nemerle.Compiler.Token;
10   -
11   -namespace Nemerle.Parser
12   -{
13   - internal partial class GrammarParser
14   - {
15   - private ParceRecoveryAttribute(tok : Token, makeAttr : (Nemerle.Compiler.Location * NameRef * Rule * Rule) -> RuleAttribute) : RuleAttribute * Token
16   - {
17   - match (tok.Next)
18   - {
19   - | RoundGroup as group =>
20   - match (group.Child)
21   - {
22   - | LooseGroup(Identifier(handlerName) as id) as looseGroup =>
23   - if (looseGroup.Next is LooseGroup)
24   - {
25   - when (looseGroup.Next == null)
26   - _ = ReportCriticalUnexpected(looseGroup, "recovery-handler-name, stopper-rule, rule-to-skip");
27   - when (looseGroup.Next.Next == null)
28   - _ = ReportCriticalUnexpected(looseGroup.Next, "recovery-handler-name, stopper-rule, rule-to-skip");
29   -
30   - def (stopperRule, _) = ParseSequence(looseGroup.Next);
31   - def (skipRule, _) = ParseSequence(looseGroup.Next.Next);
32   - def location = tok.Location + group.Location;
33   - def name = NameRef(id.Location, handlerName);
34   - def attr = makeAttr(location, name, stopperRule, skipRule);
35   - (attr, group.Next)
36   - }
37   - else
38   - ReportCriticalUnexpected(group, "(recovery-handler-name, stopper-rule, rule-to-skip)")
39   -
40   - | _ => ReportCriticalUnexpected(group, "(recovery-handler-name, stopper-rule, rule-to-skip)")
41   - }
42   - | null => ReportCriticalUnexpected(tok, "FailureRecovery(recovery-handler-name, stopper-rule, rule-to-skip)")
43   - | x => ReportCriticalUnexpected(x, "(recovery-handler-name, stopper-rule, rule-to-skip)")
44   - }
45   - }
46   -
47   - private ParseAttributes(tok : Token) : Token * list[RuleAttribute]
48   - {
49   - | SquareGroup(child) =>
50   - def parseAttr(g : Token) : RuleAttribute * Token
51   - {
52   - | LooseGroup(tok) =>
53   - def getName(tok : Token) : NameRef
54   - {
55   - match (tok.Next)
56   - {
57   - | Identifier(name) as id =>
58   - when (id.Next != null)
59   - ReportUnexpected(id.Next, "nothing");
60   -
61   - NameRef(id.Location, name)
62   -
63   - | null => ReportCriticalUnexpected(tok, "rule-name");
64   - | x => ReportCriticalUnexpected(x, "rule-name");
65   - }
66   - }
67   - def getNames(tok : Token) : list[NameRef]
68   - {
69   - def getNames(tok : Token) : list[NameRef]
70   - {
71   - match (tok)
72   - {
73   - | LooseGroup(Identifier(name) as id) => NameRef(id.Location, name) :: if (tok.Next != null) getNames(tok.Next) else [];
74   - | null => [];
75   - | x => ReportCriticalUnexpected(x, "rule-name");
76   - }
77   - }
78   - match (tok.Next)
79   - {
80   - | RoundGroup as group => getNames(group.Child)
81   - | null => ReportCriticalUnexpected(tok, "(rule-names)")
82   - | x => ReportCriticalUnexpected(x, "(rule-names)")
83   - }
84   - }
85   - match (tok)
86   - {
87   - | Identifier("Inline") => (RuleAttribute.Inline(g.Location), g.Next)
88   - | Identifier("Token") => (RuleAttribute.Token(g.Location), g.Next)
89   - | Identifier("InlineAllSubrules") => (RuleAttribute.InlineAllSubrules(g.Location), g.Next)
90   - | Identifier("OmitLocation") => (RuleAttribute.OmitLocation(g.Location), g.Next)
91   - | Identifier("StartRule") => (RuleAttribute.StartRule(g.Location), g.Next)
92   - | Identifier("Export") => (RuleAttribute.Export(g.Location), g.Next)
93   - | Identifier("Extensible") => (RuleAttribute.Extensible(g.Location), g.Next)
94   - | Operator("%") => ParceRecoveryAttribute(tok, RuleAttribute.Cut);
95   - | Identifier("FailureRecovery") => ParceRecoveryAttribute(tok, RuleAttribute.Recovery);
96   - | Operator("<") => (RuleAttribute.PrecedenceLesserThan (g.Location, getName(tok)), g.Next)
97   - | Operator(">") => (RuleAttribute.PrecedenceGreaterThan(g.Location, getName(tok)), g.Next)
98   - | Identifier("Ast") => (RuleAttribute.Ast(g.Location, getNames(tok)), g.Next)
99   - | _ => ReportCriticalUnexpected(g, RuleAttribute.GetUsages())
100   - }
101   -
102   - | _ => ReportCriticalUnexpected(tok, RuleAttribute.GetUsages())
103   - }
104   - def parseAttrs(currTok : Token, attrs = [])
105   - {
106   - if (currTok == null) attrs.Reverse()
107   - else
108   - {
109   - def (attr, next) = parseAttr(currTok);
110   - parseAttrs(next, attr :: attrs)
111   - }
112   - }
113   -
114   - (tok.Next, parseAttrs(child, []))
115   -
116   - | _ => (tok, [])
117   - }
118   - }
119   -}
112 Nemerle.Parser.Macro/GrammarParser/ParseCardinalityRule.n
... ... @@ -1,112 +0,0 @@
1   -using System;
2   -using Nemerle;
3   -using Nemerle.Collections;
4   -using Nemerle.Text;
5   -using Nemerle.Utility;
6   -using Nemerle.Compiler;
7   -using Nemerle.Compiler.Parsetree;
8   -using Nemerle.Compiler.Parsetree.PExpr;
9   -using Nemerle.Compiler.Token;
10   -
11   -namespace Nemerle.Parser
12   -{
13   - internal partial class GrammarParser
14   - {
15   - // CardinalityRule = SimpleRule ('?' / '+' / '*')?
16   - private ParseCardinalityRule(tok : Token) : Rule * Token
17   - {
18   - def (innerRule, nextTok2) = ParseSimpleRule(tok);
19   -
20   - match (nextTok2)
21   - {
22   - | Operator("?") => (Rule.Repeat(nextTok2.Location, 0, Some(1), innerRule), nextTok2.Next)
23   - | Operator("+") => (Rule.Repeat(nextTok2.Location, 1, None(), innerRule), nextTok2.Next)
24   - | Operator("*") => (Rule.Repeat(nextTok2.Location, 0, None(), innerRule), nextTok2.Next)
25   - | BracesGroup(LooseGroup(tok), _) =>
26   - def getIntValue(tok)
27   - {
28   - | IntegerLiteral(lit) when !lit.is_negative =>
29   - match (lit.AsInt)
30   - {
31   - | Some(value) => value
32   - | _ => ReportCriticalUnexpected(tok, "positive integer literal")
33   - }
34   -
35   - | _ => ReportCriticalUnexpected(tok, "positive integer literal")
36   - }
37   -
38   - def first = getIntValue(tok);
39   -
40   - match (tok.Next)
41   - {
42   - | null => (Rule.Repeat(nextTok2.Location, first, Some(first), innerRule), nextTok2.Next)
43   - | Comma as next when next.Next is Token.Keyword("_") => // {n, _} - n or more
44   - when (next.Next.Next != null)
45   - ReportUnexpected(next.Next, "EOF");
46   -
47   - (Rule.Repeat(nextTok2.Location, first, None(), innerRule), nextTok2.Next)
48   -
49   - | Comma as next => // {n, m} - from n to m
50   - def second = getIntValue(next.Next);
51   -
52   - when (second < first)
53   - ReportUnexpected(next.Next, $"a integer literal which greater or equal to $first");
54   -
55   - (Rule.Repeat(nextTok2.Location, first, Some(second), innerRule), nextTok2.Next)
56   -
57   - | _ => (innerRule, nextTok2)
58   - }
59   -
60   - | _ => (innerRule, nextTok2)
61   - }
62   - }
63   -
64   - private ParseRegularCardinalityRule(tok : Token) : RegularRule * Token
65   - {
66   - def (innerRule, nextTok2) = ParseRegularSimpleRule(tok);
67   -
68   - match (nextTok2)
69   - {
70   - | Operator("?") => (RegularRule.Repeat(nextTok2.Location, 0, Some(1), innerRule), nextTok2.Next)
71   - | Operator("+") => (RegularRule.Repeat(nextTok2.Location, 1, None(), innerRule), nextTok2.Next)
72   - | Operator("*") => (RegularRule.Repeat(nextTok2.Location, 0, None(), innerRule), nextTok2.Next)
73   - | BracesGroup(LooseGroup(tok), _) =>
74   - def getIntValue(tok)
75   - {
76   - | IntegerLiteral(lit) when !lit.is_negative =>
77   - match (lit.AsInt)
78   - {
79   - | Some(value) => value
80   - | _ => ReportCriticalUnexpected(tok, "positive integer literal")
81   - }
82   -
83   - | _ => ReportCriticalUnexpected(tok, "positive integer literal")
84   - }
85   -
86   - def first = getIntValue(tok);
87   -
88   - match (tok.Next)
89   - {
90   - | null => (RegularRule.Repeat(nextTok2.Location, first, Some(first), innerRule), nextTok2.Next)
91   - | Comma as next when next.Next is Token.Keyword("_") => // {n, _} - n or more
92   - when (next.Next.Next != null)
93   - ReportUnexpected(next.Next, "EOF");
94   -
95   - (RegularRule.Repeat(nextTok2.Location, first, None(), innerRule), nextTok2.Next)
96   -
97   - | Comma as next => // {n, m} - from n to m
98   - def second = getIntValue(next.Next);
99   -
100   - when (second < first)
101   - ReportUnexpected(next.Next, $"a integer literal which greater or equal to $first");
102   -
103   - (RegularRule.Repeat(nextTok2.Location, first, Some(second), innerRule), nextTok2.Next)
104   -
105   - | _ => (innerRule, nextTok2)
106   - }
107   -
108   - | _ => (innerRule, nextTok2)
109   - }
110   - }
111   - }
112   -}
32 Nemerle.Parser.Macro/GrammarParser/ParseChoice.n
... ... @@ -1,32 +0,0 @@
1   -using System;
2   -using Nemerle;
3   -using Nemerle.Collections;
4   -using Nemerle.Text;
5   -using Nemerle.Utility;
6   -using Nemerle.Compiler;
7   -using Nemerle.Compiler.Parsetree;
8   -using Nemerle.Compiler.Parsetree.PExpr;
9   -using Nemerle.Compiler.Token;
10   -
11   -namespace Nemerle.Parser
12   -{
13   - internal partial class GrammarParser
14   - {
15   - // OrderedChoice = Sequence ( '/' Sequence)*
16   - private ParseRegularChoice(tok : Token) : RegularRule * Token
17   - {
18   - def parseOr(tok : Token) : Token
19   - {
20   - | Operator("|") =>
21   - if (tok.Next == null) ReportCriticalUnexpected(tok, "rule")
22   - else tok.Next
23   -
24   - | _ => null
25   - }
26   -
27   - def (seqs, nextTok) = ParseOneOrMany(tok, parseOr, ParseRegularSequence, "sequence");
28   - def loc = if (seqs.IsEmpty) tok.Location else tok.Location + seqs.Last.Location;
29   - (RegularRule.Choice(loc, seqs), nextTok)
30   - }
31   - }
32   -}
70 Nemerle.Parser.Macro/GrammarParser/ParseGrammar.n
... ... @@ -1,70 +0,0 @@
1   -using System;
2   -using Nemerle;
3   -using Nemerle.Collections;
4   -using Nemerle.Text;
5   -using Nemerle.Utility;
6   -using Nemerle.Compiler;
7   -using Nemerle.Compiler.Parsetree;
8   -using Nemerle.Compiler.Parsetree.PExpr;
9   -using Nemerle.Compiler.Token;
10   -
11   -namespace Nemerle.Parser
12   -{
13   - internal partial class GrammarParser
14   - {
15   - public ParseGrammar(options : PExpr, rules : PExpr) : Grammar
16   - {
17   - def _emitDebugSources = match (options) { | <[ Options = EmitDebugSources ]> => true | _ => false }
18   - def grammar = Grammar();
19   - match (rules)
20   - { // Match grammar { ... } RawToken macro (defined above)
21   - | MacroCall(name, _ns, [SyntaxElement.RawToken(LooseGroup(BracesGroup(tokens, _)))]) => // Name * NamespaceTree.Node * list [SyntaxElement]
22   - if (name.Id != "parsergrammar")
23   - ReportCriticalUnexpected(rules, "parsergrammar { ... }")
24   - else if (tokens == null)
25   - grammar;
26   - else
27   - ParseGramar(grammar, tokens)
28   - | rule => ReportCriticalUnexpected(rule, "parsergrammar { ... }")
29   - }
30   - }
31   -
32   - private ParseGramar(grammar : Grammar, token : Token) : Grammar
33   - {
34   - //assert2(!token.ToString().Contains("using"));
35   - def grammar2 =
36   - match (token)
37   - {
38   - | LooseGroup(Keyword("using") as u) =>
39   - when (u.Next == null)
40   - _ = ReportCriticalUnexpected(u, "using parser-type;");
41   -
42   - def (expr, nextToken) = _parser.ParseExpr(Token.LooseGroup(u.Next), TokenStoppers.None);
43   -
44   - when (nextToken != null)
45   - ReportUnexpected(nextToken, "nothing");
46   -
47   - match (expr)
48   - {
49   - | <[ $(alias : name) = $parserType ]> =>
50   - grammar.Add(GrammarRef(u.Location, grammar.GrammarRefs.Length, Some(NameRef(alias.Location, alias.Id)), _typer.BindFixedType(parserType)))
51   -
52   - | _ => grammar.Add(GrammarRef(u.Location, grammar.GrammarRefs.Length, None(), _typer.BindFixedType(expr)))
53   - }
54   -
55   - | _ =>
56   - def ruleDefinition = ParseRule(token, grammar.Count);
57   -
58   - if (ruleDefinition.GetRule() == null)//TODO: WTF?!
59   - grammar
60   - else
61   - grammar.Add(ruleDefinition)
62   - };
63   -
64   - if (token.Next == null)
65   - grammar2
66   - else
67   - ParseGramar(grammar2, token.Next)
68   - }
69   - }
70   -}
31 Nemerle.Parser.Macro/GrammarParser/ParseOneOrMany.n
... ... @@ -1,31 +0,0 @@
1   -using System;
2   -using Nemerle;
3   -using Nemerle.Collections;
4   -using Nemerle.Text;
5   -using Nemerle.Utility;
6   -using Nemerle.Compiler;
7   -using Nemerle.Compiler.Parsetree;
8   -using Nemerle.Compiler.Parsetree.PExpr;
9   -using Nemerle.Compiler.Token;
10   -
11   -namespace Nemerle.Parser
12   -{
13   - internal partial class GrammarParser
14   - {
15   - private ParseOneOrMany[RuleT](
16   - tok : Token,
17   - parseDelemiter : Token -> Token,
18   - parseParser : Token -> RuleT * Token,
19   - expected : string
20   - ) : list[RuleT] * Token
21   - where RuleT : class
22   - {
23   - def result = ParseZeroOrMany(tok, parseDelemiter, parseParser);
24   -
25   - when (result[0].IsEmpty)
26   - _ = ReportCriticalUnexpected(tok, expected);
27   -
28   - result
29   - }
30   - }
31   -}
31 Nemerle.Parser.Macro/GrammarParser/ParsePredicateRule.n
... ... @@ -1,31 +0,0 @@
1   -using System;
2   -using Nemerle;
3   -using Nemerle.Collections;
4   -using Nemerle.Text;
5   -using Nemerle.Utility;
6   -using Nemerle.Compiler;
7   -using Nemerle.Compiler.Parsetree;
8   -using Nemerle.Compiler.Parsetree.PExpr;
9   -using Nemerle.Compiler.Token;
10   -
11   -namespace Nemerle.Parser
12   -{
13   - internal partial class GrammarParser
14   - {
15   - // PredicateRule = ('!' / '&')? CardinalityRule
16   - private ParsePredicateRule(tok : Token) : Rule * Token
17   - {
18   - def (rule, nextTok1) =
19   - match (tok)
20   - {
21   - | Operator("!") => (Rule.Not : Nemerle.Compiler.Location * Rule -> Rule, tok.Next)
22   - | Operator("&") => (Rule.And : Nemerle.Compiler.Location * Rule -> Rule, tok.Next)
23   - | _ => (null, tok)
24   - };
25   -
26   - def (innerRule, nextTok2) = ParseCardinalityRule(nextTok1);
27   - if (rule == null) (innerRule, nextTok2)
28   - else (rule(tok.Location, innerRule), nextTok2)
29   - }
30   - }
31   -}
56 Nemerle.Parser.Macro/GrammarParser/ParseRange.n
... ... @@ -1,56 +0,0 @@
1   -using System;
2   -using Nemerle;
3   -using Nemerle.Collections;
4   -using Nemerle.Text;
5   -using Nemerle.Utility;
6   -using Nemerle.Compiler;
7   -using Nemerle.Compiler.Parsetree;
8   -using Nemerle.Compiler.Parsetree.PExpr;
9   -using Nemerle.Compiler.Token;
10   -
11   -namespace Nemerle.Parser
12   -{
13   - internal partial class GrammarParser
14   - {
15   - // Range = Token.SquareGroup(LooseGroup(CharLiteral, Operator(".."), CharLiteral))
16   - private ParseRange(tok : Token) : RegularRule * Token
17   - {
18   - | SquareGroup(entry) =>
19   - def parseEntry(entry : Token, set : RangeSet) : RangeSet
20   - {
21   - match (entry)
22   - {
23   - | LooseGroup(CharLiteral
24   - where (Next = Operator where (name = "..", Next = CharLiteral as ch2)) as ch1) =>
25   -
26   - def resSet = set.AddRange(ch1.value, ch2.value);
27   - if (entry.Next == null) resSet
28   - else parseEntry(entry.Next, resSet)
29   -
30   - | CharLiteral(ch) =>
31   - def resSet = set.AddRange(ch, ch);
32   - if (entry.Next == null) resSet
33   - else parseEntry(entry.Next, resSet)
34   -
35   - | LooseGroup(Identifier(name))
36   - | Identifier(name) =>
37   - match(UnicodeCategoryTable.StrToUnicodeCategory(name))
38   - {
39   - | Some(cat) =>
40   - def resSet = set.AddRange(cat);
41   - if (entry.Next == null) resSet
42   - else parseEntry(entry.Next, resSet)
43   - | _ => ReportCriticalUnexpected(entry, "valid unicode class name")
44   - }
45   -
46   - | _ => ReportCriticalUnexpected(entry, "startChar .. endChar or char")
47   - }
48   - }
49   -
50   - def set = parseEntry(entry, RangeSet());
51   - (RegularRule.Chars(tok.Location, [set]), tok.Next)
52   -
53   - | _ => ReportCriticalUnexpected(tok, "[ ... ]")
54   - }
55   - }
56   -}
141 Nemerle.Parser.Macro/GrammarParser/ParseRule.n
... ... @@ -1,141 +0,0 @@
1   -using System;
2   -using Nemerle;
3   -using Nemerle.Collections;
4   -using Nemerle.Text;
5   -using Nemerle.Utility;
6   -using Nemerle.Compiler;
7   -using Nemerle.Compiler.Parsetree;
8   -using Nemerle.Compiler.Parsetree.PExpr;
9   -using Nemerle.Compiler.Token;
10   -
11   -namespace Nemerle.Parser
12   -{
13   - internal partial class GrammarParser
14   - {
15   - // Rule = RuleName ((':' Type)? '=' OrderedChoice)?
16   - private ParseRule(tok : Token, ruleId : int) : RuleDefinition
17   - {
18   - def straightenLooseGroup(tok : Token) : Token
19   - {
20   - match (tok, tok.Next)
21   - {
22   - | (LooseGroup(child1) as g1, LooseGroup(child2) as g2) when !(g1.SeparatorToken is Semicolon) =>
23   - def getLastTok(tok : Token)
24   - {
25   - if (tok.Next == null)
26   - tok
27   - else
28   - getLastTok(tok.Next)
29   - }
30   - getLastTok(child1).Next = child2; // append
31   - def newLoose = LooseGroup(g1.Location + g2.Location, child1, g2.SeparatorToken);
32   - newLoose.Next = g2.Next;
33   - g1.Next = g2.Next;
34   - straightenLooseGroup(newLoose)
35   -
36   - | _ => tok
37   - }
38   - }
39   - match (straightenLooseGroup(tok))
40   - {
41   - | LooseGroup(child) =>
42   - def (tok2, ruleAttrs) = ParseAttributes(child);
43   -
44   - //assert2(!_debug);
45   -
46   - def (expr, nextToken) = _parser.ParseExpr(if (tok2 is Identifier) LooseGroup(tok2)
47   - else tok2,
48   - TokenStoppers.All);
49   - def parseBody[RuleT](nextToken : Token, parse : Token -> RuleT * Token) : option[RuleT]
50   - {
51   - if (nextToken == null)
52   - None();
53   - else if (!(nextToken is Operator("=")))
54   - ReportCriticalUnexpected(nextToken, "('=' rule-body)? ';'");
55   - else if (nextToken.Next == null)
56   - ReportCriticalUnexpected(nextToken, "('=' rule-body)? ';'");
57   - else
58   - {
59   - def (rule, nextTok) = parse(nextToken.Next);
60   -
61   - when (nextTok != null)
62   - _ = ReportUnexpected(nextTok, "EOF");
63   - Some(rule);
64   - }
65   - }
66   -
67   - def simple(name, ty, rule)
68   - {
69   - RuleDefinition.Simple(name.Location, ruleId, NameRef(name), ruleAttrs, ty, rule)
70   - }
71   - def regular(name, rule)
72   - {
73   - RuleDefinition.Regular(name.Location, ruleId, NameRef(name), ruleAttrs, rule)
74   - }
75   - def extentionPoint(name, ty)
76   - {
77   - RuleDefinition.ExtentionPoint(name.Location, ruleId, NameRef(name), ruleAttrs, ty)
78   - }
79   - def extention(name, baseRuleName, rule)
80   - {
81   - def baseRuleName = match (baseRuleName)
82   - {
83   - | PExpr.Ref as baseRuleName => NameRef(baseRuleName);
84   - | PExpr.Member as baseRuleName => NameRef(baseRuleName.Location, [baseRuleName.obj.ToString(), baseRuleName.member.ToString()]);
85   - | _ => assert(false);
86   - }
87   - match (rule : Rule)
88   - {
89   - | Sequence((Call(callName, bindingPower) as call) :: rules) when callName == baseRuleName =>
90   - RuleDefinition.ExtentionPostfix
91   - ( name.Location
92   - , ruleId
93   - , NameRef(name)
94   - , ruleAttrs
95   - , baseRuleName
96   - , bindingPower
97   - , Rule.Sequence(rule.Location, Rule.PrefixCall(call.Location, baseRuleName, bindingPower) :: rules)
98   - )
99   -
100   - | _ =>
101   - RuleDefinition.ExtentionPrefix
102   - ( name.Location
103   - , ruleId
104   - , NameRef(name)
105   - , ruleAttrs
106   - , baseRuleName
107   - , rule
108   - )
109   - }
110   - }
111   - match (expr)
112   - {
113   - | <[ $name : $ty ]> =>
114   - match (parseBody(nextToken, ParseSequence))
115   - {
116   - | Some(rule) => simple(name, RuleType.PType(ty.Location, ty), rule)
117   - | None => extentionPoint(name, RuleType.PType(ty.Location, ty))
118   - }
119   -
120   - | <[ $name is $baseRuleName ]> =>
121   - match (parseBody(nextToken, ParseSequence))
122   - {
123   - | Some(rule) => extention(name, baseRuleName, rule)
124   - | None => ReportCriticalUnexpected(expr, "Extension can not be extensible.");
125   - }
126   -
127   - | Ref as name =>
128   - match (parseBody(nextToken, ParseRegularChoice))
129   - {
130   - | Some(rule) => regular(name, rule)
131   - | None => ReportCriticalUnexpected(expr, "Extension point must be typed.");
132   - }
133   -
134   - | _ => ReportCriticalUnexpected(tok2, "rule-name (':' return-type)?");
135   - };
136   -
137   - | _ => ReportCriticalUnexpected(tok, <# [Attributes] RuleName (("extends" RuleRef / ':' Type)? '=' OrderedChoice)?;#>)
138   - }
139   - }
140   - }
141   -}
30 Nemerle.Parser.Macro/GrammarParser/ParseSequence.n
... ... @@ -1,30 +0,0 @@
1   -using System;
2   -using Nemerle;
3   -using Nemerle.Collections;
4   -using Nemerle.Text;
5   -using Nemerle.Utility;
6   -using Nemerle.Compiler;
7   -using Nemerle.Compiler.Parsetree;
8   -using Nemerle.Compiler.Parsetree.PExpr;
9   -using Nemerle.Compiler.Token;
10   -
11   -namespace Nemerle.Parser
12   -{
13   - internal partial class GrammarParser
14   - {
15   - // Sequence = PredicateRule+
16   - private ParseSequence(tok : Token) : Rule * Token
17   - {
18   - def (seqs, nextTok) = ParseOneOrMany(tok, null, ParsePredicateRule, "PredicateRule");
19   - def loc = if (seqs.IsEmpty) tok.Location else tok.Location + seqs.Last.Location;
20   - (Rule.Sequence(loc, seqs), nextTok)
21   - }
22   -
23   - private ParseRegularSequence(tok : Token) : RegularRule * Token
24   - {
25   - def (seqs, nextTok) = ParseOneOrMany(tok, null, ParseRegularCardinalityRule, "PredicateRule");
26   - def loc = if (seqs.IsEmpty) tok.Location else tok.Location + seqs.Last.Location;
27   - (RegularRule.Sequence(loc, seqs), nextTok)
28   - }
29   - }
30   -}
120 Nemerle.Parser.Macro/GrammarParser/ParseSimpleRule.n
... ... @@ -1,120 +0,0 @@
1   -using System;
2   -using Nemerle;
3   -using Nemerle.Collections;
4   -using Nemerle.Text;
5   -using Nemerle.Utility;
6   -using Nemerle.Compiler;
7   -using Nemerle.Compiler.Parsetree;
8   -using Nemerle.Compiler.Parsetree.PExpr;
9   -using Nemerle.Compiler.Token;
10   -
11   -namespace Nemerle.Parser
12   -{
13   - internal partial class GrammarParser
14   - {
15   - private ParseName(tok : Token) : NameRef * Token
16   - {
17   - def parseName(tok)
18   - {
19   - match (tok.Next)
20   - {
21   - | Operator(".") =>
22   - match (tok.Next.Next)
23   - {
24   - | Identifier(name) =>
25   - def (names, loc, next) = parseName(tok.Next.Next);
26   - (name :: names, loc, next)
27   - | _ => ReportCriticalUnexpected(tok.Next.Next, "identifier")
28   - }
29   - | _ => ([], tok.Location, tok.Next)
30   - }
31   - }
32   - match (tok)
33   - {
34   - | Identifier(name) =>
35   - def (names, location, nextTok) = parseName(tok);
36   - def ruleName = NameRef(tok.Location + location, name :: names);
37   - assert(ruleName.PartsCount <= 2);
38   - (ruleName, nextTok)
39   -
40   - | _ => assert(false);
41   - }
42   - }
43   -
44   - private ParserRoundGroup[RuleT]
45   - ( group : RoundGroup
46   - , parse : Token -> RuleT * Token
47   - , repeatWithSeparator : Compiler.Location * int * option[int] * RuleT * RuleT -> RuleT
48   - ) : RuleT * Token
49   - {
50   - def (rule1, nextToken) = parse(group.Child);
51   -
52   - if (group.Child.Next != null)
53   - {
54   - def (rule2, nextToken) = parse(group.Child.Next);
55   - if (nextToken == null && group.Child.Next.Next == null)
56   - {
57   - match (group.Next)
58   - {
59   - | Operator("*") with min = 0
60   - | Operator("+") with min = 1 =>
61   - (repeatWithSeparator(group.Location, min, None(), rule1, rule2), group.Next.Next)
62   - | _ => ReportCriticalUnexpected(group.Next, "'*' or '+'");
63   - }
64   - }
65   - else
66   - ReportCriticalUnexpected(nextToken, "')'");
67   - }
68   - else
69   - if (nextToken == null)
70   - (rule1, group.Next)
71   - else
72   - ReportCriticalUnexpected(nextToken, "',' or ')'");
73   - }
74   -
75   - // SimpleRule = FailureRecovery '(' Identifier, OrderedChoice ')', RuleName '{' OrderedChoice '}' / RuleName / Range / Char / String / '(' OrderedChoice ')' / Empty
76   - private ParseSimpleRule(tok : Token) : Rule * Token
77   - {
78   - | Token.Operator("%") => (Rule.Cut(tok.Location), tok.Next)
79   - | Identifier =>
80   - def (ruleName, nextTok) = ParseName(tok);
81   - match (nextTok)
82   - {
83   - | BracesGroup(LooseGroup(IntegerLiteral), _)
84   - => (Rule.Call(ruleName.Location, ruleName, 0), nextTok)
85   - | BracesGroup as group => (Rule.Scope(ruleName.Location + group.Location, ruleName, ParseSequence(group.Child)[0]), group.Next)
86   - | Operator(":") as tok =>
87   - match (tok.Next)
88   - {
89   - | IntegerLiteral as tok =>
90   - match (tok.lit.AsInt)
91   - {
92   - | Some(bp) => (Rule.Call(ruleName.Location, ruleName, bp), tok.Next)
93   - | _ => ReportCriticalUnexpected(tok, "integer-literal")
94   - }
95   - | tok => ReportCriticalUnexpected(tok, "integer-literal")
96   - }
97   - | _ => (Rule.Call(ruleName.Location, ruleName, 0), nextTok)
98   - }
99   -
100   - | RoundGroup as group => ParserRoundGroup(group, ParseSequence, Rule.RepeatWithSeparator)
101   - | StringLiteral(value = str) => (Rule.String(tok.Location, str), tok.Next)
102   - | CharLiteral(ch) => (Rule.String(tok.Location, ch.ToString()), tok.Next)
103   - | null => (Rule.Sequence([]), null)
104   - | _ => (null, tok)
105   - }
106   -
107   - private ParseRegularSimpleRule(tok : Token) : RegularRule * Token
108   - {
109   - | SquareGroup as group => (ParseRange(group)[0], group.Next)
110   - | Identifier =>
111   - def (ruleName, nextTok) = ParseName(tok);
112   - (RegularRule.Call(ruleName.Location, ruleName), nextTok)
113   - | RoundGroup as group => ParserRoundGroup(group, ParseRegularChoice, RegularRule.RepeatWithSeparator)
114   - | StringLiteral(value = str) => (RegularRule.Chars(tok.Location, str.Map(ch => RangeSet().AddRange(ch, ch))), tok.Next)
115   - | CharLiteral(ch) => (RegularRule.Chars(tok.Location, [RangeSet().AddRange(ch, ch)]), tok.Next)
116   - | null => (RegularRule.Sequence([]), null)
117   - | _ => (null, tok)
118   - }
119   - }
120   -}
56 Nemerle.Parser.Macro/GrammarParser/ParseZeroOrMany.n
... ... @@ -1,56 +0,0 @@
1   -using System;
2   -using Nemerle;
3   -using Nemerle.Collections;
4   -using Nemerle.Text;
5   -using Nemerle.Utility;
6   -using Nemerle.Compiler;
7   -using Nemerle.Compiler.Parsetree;
8   -using Nemerle.Compiler.Parsetree.PExpr;
9   -using Nemerle.Compiler.Token;
10   -
11   -namespace Nemerle.Parser
12   -{
13   - internal partial class GrammarParser
14   - {
15   - private ParseZeroOrMany[RuleT](
16   - tok : Token,
17   - parseDelemiter : Token -> Token,
18   - parseParser : Token -> RuleT * Token
19   - ) : list[RuleT] * Token
20   - where RuleT : class
21   - {
22   - def tok1 = match (tok)
23   - {
24   - | LooseGroup(child) => child
25   - | _ => tok
26   - };
27   -
28   - def (seq, nextTok) = parseParser(tok1);
29   -
30   - def loop(tok, acc) : list[RuleT] * Token
31   - {
32   - def nextTok1 = if (parseDelemiter == null) tok else parseDelemiter(tok);
33   -
34   - if (nextTok1 == null)
35   - (acc, tok) // predicate not matched
36   - else
37   - {
38   - def (seq, nextTok2) = parseParser(nextTok1);
39   - if (seq == null) (acc, nextTok2)
40   - else if (nextTok2 == null) (seq :: acc, null)
41   - else loop(nextTok2, seq :: acc)
42   - }
43   - }
44   -
45   - if (seq == null)
46   - ([], tok)
47   - else if (nextTok == null)
48   - ([seq], nextTok)
49   - else
50   - {
51   - def (res, nextTok) = loop(nextTok, [seq]);
52   - (res.Rev(), nextTok)
53   - }
54   - }
55   - }
56   -}
84 Nemerle.Parser.Macro/Macro.n
... ... @@ -1,84 +0,0 @@
1   -using System;
2   -
3   -using Nemerle;
4   -using Nemerle.Collections;
5   -using Nemerle.Text;
6   -using Nemerle.Utility;
7   -using Nemerle.Compiler;
8   -using Nemerle.Compiler.Parsetree;
9   -using Nemerle.Compiler.Parsetree.PExpr;
10   -using Nemerle.Compiler.Token;
11   -using Nemerle.Imperative;
12   -
13   -namespace Nemerle.Parser
14   -{
15   - macro GrammarScope(_rules : Token)
16   - syntax ("parsergrammar", _rules)
17   - {
18   - <[ () ]>
19   - }
20   -
21   - [Nemerle.MacroUsage(Nemerle.MacroPhase.BeforeTypedMembers, Nemerle.MacroTargets.Class)]
22   - macro ParserGrammar(tb : TypeBuilder, options, rules)
23   - {
24   - GrammarImpl.BeforeTypedMembers(Macros.ImplicitCTX(), tb, options, rules);
25   - }
26   -
27   - [Nemerle.MacroUsage(Nemerle.MacroPhase.WithTypedMembers, Nemerle.MacroTargets.Class)]
28   - macro ParserGrammar(tb : TypeBuilder, _options, _rules)
29   - {
30   - GrammarImpl.WithTypedMembers(tb);
31   - }
32   -
33   - internal module GrammarImpl
34   - {
35   - public BeforeTypedMembers(typer : Typer, tb : TypeBuilder, options : PExpr, rules : PExpr) : void
36   - {
37   - try
38   - {
39   - def gc = GrammarCompiler(typer.Manager, typer.Env, tb, GrammarParser(typer, false).ParseGrammar(options, rules));
40   - tb.UserData[typeof(GrammarCompiler)] = gc;
41   - }
42   - catch
43   - {
44   - | e is GrammarException =>
45   - def loc = if (e.Location != Location.Default) e.Location else tb.Location;
46   - Message.Error(loc, e.Message);
47   - | _ is CriticalUnexpectedException => ()
48   - | _ is Nemerle.Compiler.Recovery => ()
49   - | e is Exception =>
50   - assert2(false, e.ToString());
51   - Message.Error(tb.Location, $"Exception $e");
52   - }
53   - }
54   -
55   - public WithTypedMembers(tb : TypeBuilder) : void
56   - {
57   - try
58   - {
59   - def compiler = tb.UserData[typeof(GrammarCompiler)] :> GrammarCompiler;
60   - when (compiler == null)
61   - Message.FatalError(tb.Location, "Grammar not parsed.");
62   -
63   - if (compiler.Manager.IsIntelliSenseMode)
64   - IntelliSenseSupport.AddIntelliSenseSupport(compiler);
65   - else
66   - when (compiler.AllRules)
67   - compiler.CompileGrammar();
68   -
69   - compiler.DefineAllMethods();
70   - }
71   - catch
72   - {
73   - | e is GrammarException =>
74   - def loc = if (e.Location != Location.Default) e.Location else tb.Location;
75   - Message.Error(loc, e.Message);
76   - | _ is CriticalUnexpectedException => ()
77   - | _ is Nemerle.Compiler.Recovery => ()
78   - | e is Exception =>
79   - assert2(false, e.ToString());
80   - Message.Error(tb.Location, $"Exception $e");
81   - }
82   - }
83   - }
84   -}
2  Nemerle.Parser.Macro/N2Source.n
@@ -112,7 +112,6 @@ namespace Nemerle.Parser
112 112 | e is GrammarException =>
113 113 def loc = if (e.Location != Location.Default) e.Location else sm.TypeBuilder.Location;
114 114 Message.Error(loc, e.Message);
115   - | _ is CriticalUnexpectedException => ()
116 115 | _ is Nemerle.Compiler.Recovery => ()
117 116 | e is Exception =>
118 117 assert2(false, e.ToString());
@@ -142,7 +141,6 @@ namespace Nemerle.Parser
142 141 | e is GrammarException =>
143 142 def loc = if (e.Location != Location.Default) e.Location else sm.TypeBuilder.Location;
144 143 Message.Error(loc, e.Message);
145   - | _ is CriticalUnexpectedException => ()
146 144 | _ is Nemerle.Compiler.Recovery => ()
147 145 | e is Exception =>
148 146 assert2(false, e.ToString());
43 Nemerle.Parser.Macro/Nemerle.Parser.Macro.nproj
@@ -255,51 +255,9 @@
255 255 <Compile Include="FSM\FSMBuilder.n">
256 256 <SubType>Code</SubType>
257 257 </Compile>
258   - <Compile Include="GrammarParser\Constructor.n">
259   - <SubType>Code</SubType>
260   - </Compile>
261   - <Compile Include="GrammarParser\ErrorReport.n">
262   - <SubType>Code</SubType>
263   - </Compile>
264   - <Compile Include="GrammarParser\ParseAttribute.n">
265   - <SubType>Code</SubType>
266   - </Compile>
267   - <Compile Include="GrammarParser\ParseCardinalityRule.n">
268   - <SubType>Code</SubType>
269   - </Compile>
270   - <Compile Include="GrammarParser\ParseGrammar.n">
271   - <SubType>Code</SubType>
272   - </Compile>
273   - <Compile Include="GrammarParser\ParseOneOrMany.n">
274   - <SubType>Code</SubType>
275   - </Compile>
276   - <Compile Include="GrammarParser\ParseChoice.n">
277   - <SubType>Code</SubType>
278   - </Compile>
279   - <Compile Include="GrammarParser\ParsePredicateRule.n">
280   - <SubType>Code</SubType>
281   - </Compile>
282   - <Compile Include="GrammarParser\ParseRange.n">
283   - <SubType>Code</SubType>
284   - </Compile>
285   - <Compile Include="GrammarParser\ParseRule.n">
286   - <SubType>Code</SubType>
287   - </Compile>
288   - <Compile Include="GrammarParser\ParseSequence.n">
289   - <SubType>Code</SubType>
290   - </Compile>
291   - <Compile Include="GrammarParser\ParseSimpleRule.n">
292   - <SubType>Code</SubType>
293   - </Compile>
294   - <Compile Include="GrammarParser\ParseZeroOrMany.n">
295   - <SubType>Code</SubType>
296   - </Compile>
297 258 <Compile Include="IntelliSenseSupport.n">
298 259 <SubType>Code</SubType>
299 260 </Compile>
300   - <Compile Include="Macro.n">
301   - <SubType>Code</SubType>
302   - </Compile>
303 261 <Compile Include="N2Source.n">
304 262 <SubType>Code</SubType>
305 263 </Compile>
@@ -317,7 +275,6 @@
317 275 <Folder Include="Compiler\GrammarCompiler\Optimization" />
318 276 <Folder Include="Compiler\RuleCompiler\" />
319 277 <Folder Include="FSM\" />
320   - <Folder Include="GrammarParser" />
321 278 <Folder Include="Properties\" />
322 279 </ItemGroup>
323 280 <ItemGroup>

0 comments on commit 9b6425c

Please sign in to comment.
Something went wrong with that request. Please try again.