Skip to content
Browse files

Work on syntax. No more support for old syntax.

  • Loading branch information...
1 parent ff56358 commit 9b6425ced5ed88a99785e47afb0eccdfc9ae66c9 @rampelstinskin committed Apr 23, 2012
View
39 Nemerle.Parser.Macro/GrammarParser/Constructor.n
@@ -1,39 +0,0 @@
-using System;
-using Nemerle;
-using Nemerle.Collections;
-using Nemerle.Text;
-using Nemerle.Utility;
-using Nemerle.Compiler;
-using Nemerle.Compiler.Parsetree;
-using Nemerle.Compiler.Parsetree.PExpr;
-using Nemerle.Compiler.Token;
-
-namespace Nemerle.Parser
-{
- internal partial class GrammarParser
- {
- // Rule = RuleName ((':' Type)? '=' OrderedChoice)?
- // OrderedChoice = Sequence ('/' Sequence)*
- // Sequence = PredicateRule+
- // PredicateRule = ('!' / '&')? CardinalityRule
- // CardinalityRule = SimpleRule ('?' / '+' / '*')?
- // SimpleRule = FailureRecovery '(' OrderedChoice ')', RuleName '{' OrderedChoice '}' / RuleName / Range / Char / String / '(' OrderedChoice ')' / Empty
- // RuleName = Token.Identifier(name)
- // Char = Token.CharLiteral
- // String = Token.StringLiteral
- // Range = Token.SquareGroup(LooseGroup(CharLiteral, Operator(".."), CharLiteral))
- // Eq = Token.Operator("=")
- // Empty = Token.Identifier("Empty")
-
- private _debug : bool;
- private _typer : Typer;
- private _parser : MainParser;
-
- public this(typer : Typer, debug : bool)
- {
- _typer = typer;
- _parser = MainParser(typer.Env);
- _debug = debug;
- }
- }
-}
View
41 Nemerle.Parser.Macro/GrammarParser/ErrorReport.n
@@ -1,41 +0,0 @@
-using System;
-using Nemerle;
-using Nemerle.Collections;
-using Nemerle.Text;
-using Nemerle.Utility;
-using Nemerle.Compiler;
-using Nemerle.Compiler.Parsetree;
-using Nemerle.Compiler.Parsetree.PExpr;
-using Nemerle.Compiler.Token;
-
-namespace Nemerle.Parser
-{
- public class CriticalUnexpectedException : Exception { }
-
- internal partial class GrammarParser
- {
- ReportCriticalUnexpected[T](token : Token, expected : string) : T
- {
- ReportUnexpected(token, expected);
- throw CriticalUnexpectedException()
- }
-
- ReportUnexpected(token : Token, expected : string) : void
- {
- assert2(!_debug);
- Message.Error(token.Location, $"expected «$expected» but found «$token» ($(token.GetType().Name))");
- }
-
- ReportUnexpected(expr : PExpr, expected : string) : void
- {
- assert2(!_debug);
- Message.Error(expr.Location, $"expected $expected but found $expr ($(expr.GetType().Name))");
- }
-
- ReportCriticalUnexpected[T](expr : PExpr, expected : string) : T
- {
- ReportUnexpected(expr, expected);
- throw CriticalUnexpectedException()
- }
- }
-}
View
119 Nemerle.Parser.Macro/GrammarParser/ParseAttribute.n
@@ -1,119 +0,0 @@
-using System;
-using Nemerle;
-using Nemerle.Collections;
-using Nemerle.Text;
-using Nemerle.Utility;
-using Nemerle.Compiler;
-using Nemerle.Compiler.Parsetree;
-using Nemerle.Compiler.Parsetree.PExpr;
-using Nemerle.Compiler.Token;
-
-namespace Nemerle.Parser
-{
- internal partial class GrammarParser
- {
- private ParceRecoveryAttribute(tok : Token, makeAttr : (Nemerle.Compiler.Location * NameRef * Rule * Rule) -> RuleAttribute) : RuleAttribute * Token
- {
- match (tok.Next)
- {
- | RoundGroup as group =>
- match (group.Child)
- {
- | LooseGroup(Identifier(handlerName) as id) as looseGroup =>
- if (looseGroup.Next is LooseGroup)
- {
- when (looseGroup.Next == null)
- _ = ReportCriticalUnexpected(looseGroup, "recovery-handler-name, stopper-rule, rule-to-skip");
- when (looseGroup.Next.Next == null)
- _ = ReportCriticalUnexpected(looseGroup.Next, "recovery-handler-name, stopper-rule, rule-to-skip");
-
- def (stopperRule, _) = ParseSequence(looseGroup.Next);
- def (skipRule, _) = ParseSequence(looseGroup.Next.Next);
- def location = tok.Location + group.Location;
- def name = NameRef(id.Location, handlerName);
- def attr = makeAttr(location, name, stopperRule, skipRule);
- (attr, group.Next)
- }
- else
- ReportCriticalUnexpected(group, "(recovery-handler-name, stopper-rule, rule-to-skip)")
-
- | _ => ReportCriticalUnexpected(group, "(recovery-handler-name, stopper-rule, rule-to-skip)")
- }
- | null => ReportCriticalUnexpected(tok, "FailureRecovery(recovery-handler-name, stopper-rule, rule-to-skip)")
- | x => ReportCriticalUnexpected(x, "(recovery-handler-name, stopper-rule, rule-to-skip)")
- }
- }
-
- private ParseAttributes(tok : Token) : Token * list[RuleAttribute]
- {
- | SquareGroup(child) =>
- def parseAttr(g : Token) : RuleAttribute * Token
- {
- | LooseGroup(tok) =>
- def getName(tok : Token) : NameRef
- {
- match (tok.Next)
- {
- | Identifier(name) as id =>
- when (id.Next != null)
- ReportUnexpected(id.Next, "nothing");
-
- NameRef(id.Location, name)
-
- | null => ReportCriticalUnexpected(tok, "rule-name");
- | x => ReportCriticalUnexpected(x, "rule-name");
- }
- }
- def getNames(tok : Token) : list[NameRef]
- {
- def getNames(tok : Token) : list[NameRef]
- {
- match (tok)
- {
- | LooseGroup(Identifier(name) as id) => NameRef(id.Location, name) :: if (tok.Next != null) getNames(tok.Next) else [];
- | null => [];
- | x => ReportCriticalUnexpected(x, "rule-name");
- }
- }
- match (tok.Next)
- {
- | RoundGroup as group => getNames(group.Child)
- | null => ReportCriticalUnexpected(tok, "(rule-names)")
- | x => ReportCriticalUnexpected(x, "(rule-names)")
- }
- }
- match (tok)
- {
- | Identifier("Inline") => (RuleAttribute.Inline(g.Location), g.Next)
- | Identifier("Token") => (RuleAttribute.Token(g.Location), g.Next)
- | Identifier("InlineAllSubrules") => (RuleAttribute.InlineAllSubrules(g.Location), g.Next)
- | Identifier("OmitLocation") => (RuleAttribute.OmitLocation(g.Location), g.Next)
- | Identifier("StartRule") => (RuleAttribute.StartRule(g.Location), g.Next)
- | Identifier("Export") => (RuleAttribute.Export(g.Location), g.Next)
- | Identifier("Extensible") => (RuleAttribute.Extensible(g.Location), g.Next)
- | Operator("%") => ParceRecoveryAttribute(tok, RuleAttribute.Cut);
- | Identifier("FailureRecovery") => ParceRecoveryAttribute(tok, RuleAttribute.Recovery);
- | Operator("<") => (RuleAttribute.PrecedenceLesserThan (g.Location, getName(tok)), g.Next)
- | Operator(">") => (RuleAttribute.PrecedenceGreaterThan(g.Location, getName(tok)), g.Next)
- | Identifier("Ast") => (RuleAttribute.Ast(g.Location, getNames(tok)), g.Next)
- | _ => ReportCriticalUnexpected(g, RuleAttribute.GetUsages())
- }
-
- | _ => ReportCriticalUnexpected(tok, RuleAttribute.GetUsages())
- }
- def parseAttrs(currTok : Token, attrs = [])
- {
- if (currTok == null) attrs.Reverse()
- else
- {
- def (attr, next) = parseAttr(currTok);
- parseAttrs(next, attr :: attrs)
- }
- }
-
- (tok.Next, parseAttrs(child, []))
-
- | _ => (tok, [])
- }
- }
-}
View
112 Nemerle.Parser.Macro/GrammarParser/ParseCardinalityRule.n
@@ -1,112 +0,0 @@
-using System;
-using Nemerle;
-using Nemerle.Collections;
-using Nemerle.Text;
-using Nemerle.Utility;
-using Nemerle.Compiler;
-using Nemerle.Compiler.Parsetree;
-using Nemerle.Compiler.Parsetree.PExpr;
-using Nemerle.Compiler.Token;
-
-namespace Nemerle.Parser
-{
- internal partial class GrammarParser
- {
- // CardinalityRule = SimpleRule ('?' / '+' / '*')?
- private ParseCardinalityRule(tok : Token) : Rule * Token
- {
- def (innerRule, nextTok2) = ParseSimpleRule(tok);
-
- match (nextTok2)
- {
- | Operator("?") => (Rule.Repeat(nextTok2.Location, 0, Some(1), innerRule), nextTok2.Next)
- | Operator("+") => (Rule.Repeat(nextTok2.Location, 1, None(), innerRule), nextTok2.Next)
- | Operator("*") => (Rule.Repeat(nextTok2.Location, 0, None(), innerRule), nextTok2.Next)
- | BracesGroup(LooseGroup(tok), _) =>
- def getIntValue(tok)
- {
- | IntegerLiteral(lit) when !lit.is_negative =>
- match (lit.AsInt)
- {
- | Some(value) => value
- | _ => ReportCriticalUnexpected(tok, "positive integer literal")
- }
-
- | _ => ReportCriticalUnexpected(tok, "positive integer literal")
- }
-
- def first = getIntValue(tok);
-
- match (tok.Next)
- {
- | null => (Rule.Repeat(nextTok2.Location, first, Some(first), innerRule), nextTok2.Next)
- | Comma as next when next.Next is Token.Keyword("_") => // {n, _} - n or more
- when (next.Next.Next != null)
- ReportUnexpected(next.Next, "EOF");
-
- (Rule.Repeat(nextTok2.Location, first, None(), innerRule), nextTok2.Next)
-
- | Comma as next => // {n, m} - from n to m
- def second = getIntValue(next.Next);
-
- when (second < first)
- ReportUnexpected(next.Next, $"a integer literal which greater or equal to $first");
-
- (Rule.Repeat(nextTok2.Location, first, Some(second), innerRule), nextTok2.Next)
-
- | _ => (innerRule, nextTok2)
- }
-
- | _ => (innerRule, nextTok2)
- }
- }
-
- private ParseRegularCardinalityRule(tok : Token) : RegularRule * Token
- {
- def (innerRule, nextTok2) = ParseRegularSimpleRule(tok);
-
- match (nextTok2)
- {
- | Operator("?") => (RegularRule.Repeat(nextTok2.Location, 0, Some(1), innerRule), nextTok2.Next)
- | Operator("+") => (RegularRule.Repeat(nextTok2.Location, 1, None(), innerRule), nextTok2.Next)
- | Operator("*") => (RegularRule.Repeat(nextTok2.Location, 0, None(), innerRule), nextTok2.Next)
- | BracesGroup(LooseGroup(tok), _) =>
- def getIntValue(tok)
- {
- | IntegerLiteral(lit) when !lit.is_negative =>
- match (lit.AsInt)
- {
- | Some(value) => value
- | _ => ReportCriticalUnexpected(tok, "positive integer literal")
- }
-
- | _ => ReportCriticalUnexpected(tok, "positive integer literal")
- }
-
- def first = getIntValue(tok);
-
- match (tok.Next)
- {
- | null => (RegularRule.Repeat(nextTok2.Location, first, Some(first), innerRule), nextTok2.Next)
- | Comma as next when next.Next is Token.Keyword("_") => // {n, _} - n or more
- when (next.Next.Next != null)
- ReportUnexpected(next.Next, "EOF");
-
- (RegularRule.Repeat(nextTok2.Location, first, None(), innerRule), nextTok2.Next)
-
- | Comma as next => // {n, m} - from n to m
- def second = getIntValue(next.Next);
-
- when (second < first)
- ReportUnexpected(next.Next, $"a integer literal which greater or equal to $first");
-
- (RegularRule.Repeat(nextTok2.Location, first, Some(second), innerRule), nextTok2.Next)
-
- | _ => (innerRule, nextTok2)
- }
-
- | _ => (innerRule, nextTok2)
- }
- }
- }
-}
View
32 Nemerle.Parser.Macro/GrammarParser/ParseChoice.n
@@ -1,32 +0,0 @@
-using System;
-using Nemerle;
-using Nemerle.Collections;
-using Nemerle.Text;
-using Nemerle.Utility;
-using Nemerle.Compiler;
-using Nemerle.Compiler.Parsetree;
-using Nemerle.Compiler.Parsetree.PExpr;
-using Nemerle.Compiler.Token;
-
-namespace Nemerle.Parser
-{
- internal partial class GrammarParser
- {
- // OrderedChoice = Sequence ( '/' Sequence)*
- private ParseRegularChoice(tok : Token) : RegularRule * Token
- {
- def parseOr(tok : Token) : Token
- {
- | Operator("|") =>
- if (tok.Next == null) ReportCriticalUnexpected(tok, "rule")
- else tok.Next
-
- | _ => null
- }
-
- def (seqs, nextTok) = ParseOneOrMany(tok, parseOr, ParseRegularSequence, "sequence");
- def loc = if (seqs.IsEmpty) tok.Location else tok.Location + seqs.Last.Location;
- (RegularRule.Choice(loc, seqs), nextTok)
- }
- }
-}
View
70 Nemerle.Parser.Macro/GrammarParser/ParseGrammar.n
@@ -1,70 +0,0 @@
-using System;
-using Nemerle;
-using Nemerle.Collections;
-using Nemerle.Text;
-using Nemerle.Utility;
-using Nemerle.Compiler;
-using Nemerle.Compiler.Parsetree;
-using Nemerle.Compiler.Parsetree.PExpr;
-using Nemerle.Compiler.Token;
-
-namespace Nemerle.Parser
-{
- internal partial class GrammarParser
- {
- public ParseGrammar(options : PExpr, rules : PExpr) : Grammar
- {
- def _emitDebugSources = match (options) { | <[ Options = EmitDebugSources ]> => true | _ => false }
- def grammar = Grammar();
- match (rules)
- { // Match grammar { ... } RawToken macro (defined above)
- | MacroCall(name, _ns, [SyntaxElement.RawToken(LooseGroup(BracesGroup(tokens, _)))]) => // Name * NamespaceTree.Node * list [SyntaxElement]
- if (name.Id != "parsergrammar")
- ReportCriticalUnexpected(rules, "parsergrammar { ... }")
- else if (tokens == null)
- grammar;
- else
- ParseGramar(grammar, tokens)
- | rule => ReportCriticalUnexpected(rule, "parsergrammar { ... }")
- }
- }
-
- private ParseGramar(grammar : Grammar, token : Token) : Grammar
- {
- //assert2(!token.ToString().Contains("using"));
- def grammar2 =
- match (token)
- {
- | LooseGroup(Keyword("using") as u) =>
- when (u.Next == null)
- _ = ReportCriticalUnexpected(u, "using parser-type;");
-
- def (expr, nextToken) = _parser.ParseExpr(Token.LooseGroup(u.Next), TokenStoppers.None);
-
- when (nextToken != null)
- ReportUnexpected(nextToken, "nothing");
-
- match (expr)
- {
- | <[ $(alias : name) = $parserType ]> =>
- grammar.Add(GrammarRef(u.Location, grammar.GrammarRefs.Length, Some(NameRef(alias.Location, alias.Id)), _typer.BindFixedType(parserType)))
-
- | _ => grammar.Add(GrammarRef(u.Location, grammar.GrammarRefs.Length, None(), _typer.BindFixedType(expr)))
- }
-
- | _ =>
- def ruleDefinition = ParseRule(token, grammar.Count);
-
- if (ruleDefinition.GetRule() == null)//TODO: WTF?!
- grammar
- else
- grammar.Add(ruleDefinition)
- };
-
- if (token.Next == null)
- grammar2
- else
- ParseGramar(grammar2, token.Next)
- }
- }
-}
View
31 Nemerle.Parser.Macro/GrammarParser/ParseOneOrMany.n
@@ -1,31 +0,0 @@
-using System;
-using Nemerle;
-using Nemerle.Collections;
-using Nemerle.Text;
-using Nemerle.Utility;
-using Nemerle.Compiler;
-using Nemerle.Compiler.Parsetree;
-using Nemerle.Compiler.Parsetree.PExpr;
-using Nemerle.Compiler.Token;
-
-namespace Nemerle.Parser
-{
- internal partial class GrammarParser
- {
- private ParseOneOrMany[RuleT](
- tok : Token,
- parseDelemiter : Token -> Token,
- parseParser : Token -> RuleT * Token,
- expected : string
- ) : list[RuleT] * Token
- where RuleT : class
- {
- def result = ParseZeroOrMany(tok, parseDelemiter, parseParser);
-
- when (result[0].IsEmpty)
- _ = ReportCriticalUnexpected(tok, expected);
-
- result
- }
- }
-}
View
31 Nemerle.Parser.Macro/GrammarParser/ParsePredicateRule.n
@@ -1,31 +0,0 @@
-using System;
-using Nemerle;
-using Nemerle.Collections;
-using Nemerle.Text;
-using Nemerle.Utility;
-using Nemerle.Compiler;
-using Nemerle.Compiler.Parsetree;
-using Nemerle.Compiler.Parsetree.PExpr;
-using Nemerle.Compiler.Token;
-
-namespace Nemerle.Parser
-{
- internal partial class GrammarParser
- {
- // PredicateRule = ('!' / '&')? CardinalityRule
- private ParsePredicateRule(tok : Token) : Rule * Token
- {
- def (rule, nextTok1) =
- match (tok)
- {
- | Operator("!") => (Rule.Not : Nemerle.Compiler.Location * Rule -> Rule, tok.Next)
- | Operator("&") => (Rule.And : Nemerle.Compiler.Location * Rule -> Rule, tok.Next)
- | _ => (null, tok)
- };
-
- def (innerRule, nextTok2) = ParseCardinalityRule(nextTok1);
- if (rule == null) (innerRule, nextTok2)
- else (rule(tok.Location, innerRule), nextTok2)
- }
- }
-}
View
56 Nemerle.Parser.Macro/GrammarParser/ParseRange.n
@@ -1,56 +0,0 @@
-using System;
-using Nemerle;
-using Nemerle.Collections;
-using Nemerle.Text;
-using Nemerle.Utility;
-using Nemerle.Compiler;
-using Nemerle.Compiler.Parsetree;
-using Nemerle.Compiler.Parsetree.PExpr;
-using Nemerle.Compiler.Token;
-
-namespace Nemerle.Parser
-{
- internal partial class GrammarParser
- {
- // Range = Token.SquareGroup(LooseGroup(CharLiteral, Operator(".."), CharLiteral))
- private ParseRange(tok : Token) : RegularRule * Token
- {
- | SquareGroup(entry) =>
- def parseEntry(entry : Token, set : RangeSet) : RangeSet
- {
- match (entry)
- {
- | LooseGroup(CharLiteral
- where (Next = Operator where (name = "..", Next = CharLiteral as ch2)) as ch1) =>
-
- def resSet = set.AddRange(ch1.value, ch2.value);
- if (entry.Next == null) resSet
- else parseEntry(entry.Next, resSet)
-
- | CharLiteral(ch) =>
- def resSet = set.AddRange(ch, ch);
- if (entry.Next == null) resSet
- else parseEntry(entry.Next, resSet)
-
- | LooseGroup(Identifier(name))
- | Identifier(name) =>
- match(UnicodeCategoryTable.StrToUnicodeCategory(name))
- {
- | Some(cat) =>
- def resSet = set.AddRange(cat);
- if (entry.Next == null) resSet
- else parseEntry(entry.Next, resSet)
- | _ => ReportCriticalUnexpected(entry, "valid unicode class name")
- }
-
- | _ => ReportCriticalUnexpected(entry, "startChar .. endChar or char")
- }
- }
-
- def set = parseEntry(entry, RangeSet());
- (RegularRule.Chars(tok.Location, [set]), tok.Next)
-
- | _ => ReportCriticalUnexpected(tok, "[ ... ]")
- }
- }
-}
View
141 Nemerle.Parser.Macro/GrammarParser/ParseRule.n
@@ -1,141 +0,0 @@
-using System;
-using Nemerle;
-using Nemerle.Collections;
-using Nemerle.Text;
-using Nemerle.Utility;
-using Nemerle.Compiler;
-using Nemerle.Compiler.Parsetree;
-using Nemerle.Compiler.Parsetree.PExpr;
-using Nemerle.Compiler.Token;
-
-namespace Nemerle.Parser
-{
- internal partial class GrammarParser
- {
- // Rule = RuleName ((':' Type)? '=' OrderedChoice)?
- private ParseRule(tok : Token, ruleId : int) : RuleDefinition
- {
- def straightenLooseGroup(tok : Token) : Token
- {
- match (tok, tok.Next)
- {
- | (LooseGroup(child1) as g1, LooseGroup(child2) as g2) when !(g1.SeparatorToken is Semicolon) =>
- def getLastTok(tok : Token)
- {
- if (tok.Next == null)
- tok
- else
- getLastTok(tok.Next)
- }
- getLastTok(child1).Next = child2; // append
- def newLoose = LooseGroup(g1.Location + g2.Location, child1, g2.SeparatorToken);
- newLoose.Next = g2.Next;
- g1.Next = g2.Next;
- straightenLooseGroup(newLoose)
-
- | _ => tok
- }
- }
- match (straightenLooseGroup(tok))
- {
- | LooseGroup(child) =>
- def (tok2, ruleAttrs) = ParseAttributes(child);
-
- //assert2(!_debug);
-
- def (expr, nextToken) = _parser.ParseExpr(if (tok2 is Identifier) LooseGroup(tok2)
- else tok2,
- TokenStoppers.All);
- def parseBody[RuleT](nextToken : Token, parse : Token -> RuleT * Token) : option[RuleT]
- {
- if (nextToken == null)
- None();
- else if (!(nextToken is Operator("=")))
- ReportCriticalUnexpected(nextToken, "('=' rule-body)? ';'");
- else if (nextToken.Next == null)
- ReportCriticalUnexpected(nextToken, "('=' rule-body)? ';'");
- else
- {
- def (rule, nextTok) = parse(nextToken.Next);
-
- when (nextTok != null)
- _ = ReportUnexpected(nextTok, "EOF");
- Some(rule);
- }
- }
-
- def simple(name, ty, rule)
- {
- RuleDefinition.Simple(name.Location, ruleId, NameRef(name), ruleAttrs, ty, rule)
- }
- def regular(name, rule)
- {
- RuleDefinition.Regular(name.Location, ruleId, NameRef(name), ruleAttrs, rule)
- }
- def extentionPoint(name, ty)
- {
- RuleDefinition.ExtentionPoint(name.Location, ruleId, NameRef(name), ruleAttrs, ty)
- }
- def extention(name, baseRuleName, rule)
- {
- def baseRuleName = match (baseRuleName)
- {
- | PExpr.Ref as baseRuleName => NameRef(baseRuleName);
- | PExpr.Member as baseRuleName => NameRef(baseRuleName.Location, [baseRuleName.obj.ToString(), baseRuleName.member.ToString()]);
- | _ => assert(false);
- }
- match (rule : Rule)
- {
- | Sequence((Call(callName, bindingPower) as call) :: rules) when callName == baseRuleName =>
- RuleDefinition.ExtentionPostfix
- ( name.Location
- , ruleId
- , NameRef(name)
- , ruleAttrs
- , baseRuleName
- , bindingPower
- , Rule.Sequence(rule.Location, Rule.PrefixCall(call.Location, baseRuleName, bindingPower) :: rules)
- )
-
- | _ =>
- RuleDefinition.ExtentionPrefix
- ( name.Location
- , ruleId
- , NameRef(name)
- , ruleAttrs
- , baseRuleName
- , rule
- )
- }
- }
- match (expr)
- {
- | <[ $name : $ty ]> =>
- match (parseBody(nextToken, ParseSequence))
- {
- | Some(rule) => simple(name, RuleType.PType(ty.Location, ty), rule)
- | None => extentionPoint(name, RuleType.PType(ty.Location, ty))
- }
-
- | <[ $name is $baseRuleName ]> =>
- match (parseBody(nextToken, ParseSequence))
- {
- | Some(rule) => extention(name, baseRuleName, rule)
- | None => ReportCriticalUnexpected(expr, "Extension can not be extensible.");
- }
-
- | Ref as name =>
- match (parseBody(nextToken, ParseRegularChoice))
- {
- | Some(rule) => regular(name, rule)
- | None => ReportCriticalUnexpected(expr, "Extension point must be typed.");
- }
-
- | _ => ReportCriticalUnexpected(tok2, "rule-name (':' return-type)?");
- };
-
- | _ => ReportCriticalUnexpected(tok, <# [Attributes] RuleName (("extends" RuleRef / ':' Type)? '=' OrderedChoice)?;#>)
- }
- }
- }
-}
View
30 Nemerle.Parser.Macro/GrammarParser/ParseSequence.n
@@ -1,30 +0,0 @@
-using System;
-using Nemerle;
-using Nemerle.Collections;
-using Nemerle.Text;
-using Nemerle.Utility;
-using Nemerle.Compiler;
-using Nemerle.Compiler.Parsetree;
-using Nemerle.Compiler.Parsetree.PExpr;
-using Nemerle.Compiler.Token;
-
-namespace Nemerle.Parser
-{
- internal partial class GrammarParser
- {
- // Sequence = PredicateRule+
- private ParseSequence(tok : Token) : Rule * Token
- {
- def (seqs, nextTok) = ParseOneOrMany(tok, null, ParsePredicateRule, "PredicateRule");
- def loc = if (seqs.IsEmpty) tok.Location else tok.Location + seqs.Last.Location;
- (Rule.Sequence(loc, seqs), nextTok)
- }
-
- private ParseRegularSequence(tok : Token) : RegularRule * Token
- {
- def (seqs, nextTok) = ParseOneOrMany(tok, null, ParseRegularCardinalityRule, "PredicateRule");
- def loc = if (seqs.IsEmpty) tok.Location else tok.Location + seqs.Last.Location;
- (RegularRule.Sequence(loc, seqs), nextTok)
- }
- }
-}
View
120 Nemerle.Parser.Macro/GrammarParser/ParseSimpleRule.n
@@ -1,120 +0,0 @@
-using System;
-using Nemerle;
-using Nemerle.Collections;
-using Nemerle.Text;
-using Nemerle.Utility;
-using Nemerle.Compiler;
-using Nemerle.Compiler.Parsetree;
-using Nemerle.Compiler.Parsetree.PExpr;
-using Nemerle.Compiler.Token;
-
-namespace Nemerle.Parser
-{
- internal partial class GrammarParser
- {
- private ParseName(tok : Token) : NameRef * Token
- {
- def parseName(tok)
- {
- match (tok.Next)
- {
- | Operator(".") =>
- match (tok.Next.Next)
- {
- | Identifier(name) =>
- def (names, loc, next) = parseName(tok.Next.Next);
- (name :: names, loc, next)
- | _ => ReportCriticalUnexpected(tok.Next.Next, "identifier")
- }
- | _ => ([], tok.Location, tok.Next)
- }
- }
- match (tok)
- {
- | Identifier(name) =>
- def (names, location, nextTok) = parseName(tok);
- def ruleName = NameRef(tok.Location + location, name :: names);
- assert(ruleName.PartsCount <= 2);
- (ruleName, nextTok)
-
- | _ => assert(false);
- }
- }
-
- private ParserRoundGroup[RuleT]
- ( group : RoundGroup
- , parse : Token -> RuleT * Token
- , repeatWithSeparator : Compiler.Location * int * option[int] * RuleT * RuleT -> RuleT
- ) : RuleT * Token
- {
- def (rule1, nextToken) = parse(group.Child);
-
- if (group.Child.Next != null)
- {
- def (rule2, nextToken) = parse(group.Child.Next);
- if (nextToken == null && group.Child.Next.Next == null)
- {
- match (group.Next)
- {
- | Operator("*") with min = 0
- | Operator("+") with min = 1 =>
- (repeatWithSeparator(group.Location, min, None(), rule1, rule2), group.Next.Next)
- | _ => ReportCriticalUnexpected(group.Next, "'*' or '+'");
- }
- }
- else
- ReportCriticalUnexpected(nextToken, "')'");
- }
- else
- if (nextToken == null)
- (rule1, group.Next)
- else
- ReportCriticalUnexpected(nextToken, "',' or ')'");
- }
-
- // SimpleRule = FailureRecovery '(' Identifier, OrderedChoice ')', RuleName '{' OrderedChoice '}' / RuleName / Range / Char / String / '(' OrderedChoice ')' / Empty
- private ParseSimpleRule(tok : Token) : Rule * Token
- {
- | Token.Operator("%") => (Rule.Cut(tok.Location), tok.Next)
- | Identifier =>
- def (ruleName, nextTok) = ParseName(tok);
- match (nextTok)
- {
- | BracesGroup(LooseGroup(IntegerLiteral), _)
- => (Rule.Call(ruleName.Location, ruleName, 0), nextTok)
- | BracesGroup as group => (Rule.Scope(ruleName.Location + group.Location, ruleName, ParseSequence(group.Child)[0]), group.Next)
- | Operator(":") as tok =>
- match (tok.Next)
- {
- | IntegerLiteral as tok =>
- match (tok.lit.AsInt)
- {
- | Some(bp) => (Rule.Call(ruleName.Location, ruleName, bp), tok.Next)
- | _ => ReportCriticalUnexpected(tok, "integer-literal")
- }
- | tok => ReportCriticalUnexpected(tok, "integer-literal")
- }
- | _ => (Rule.Call(ruleName.Location, ruleName, 0), nextTok)
- }
-
- | RoundGroup as group => ParserRoundGroup(group, ParseSequence, Rule.RepeatWithSeparator)
- | StringLiteral(value = str) => (Rule.String(tok.Location, str), tok.Next)
- | CharLiteral(ch) => (Rule.String(tok.Location, ch.ToString()), tok.Next)
- | null => (Rule.Sequence([]), null)
- | _ => (null, tok)
- }
-
- private ParseRegularSimpleRule(tok : Token) : RegularRule * Token
- {
- | SquareGroup as group => (ParseRange(group)[0], group.Next)
- | Identifier =>
- def (ruleName, nextTok) = ParseName(tok);
- (RegularRule.Call(ruleName.Location, ruleName), nextTok)
- | RoundGroup as group => ParserRoundGroup(group, ParseRegularChoice, RegularRule.RepeatWithSeparator)
- | StringLiteral(value = str) => (RegularRule.Chars(tok.Location, str.Map(ch => RangeSet().AddRange(ch, ch))), tok.Next)
- | CharLiteral(ch) => (RegularRule.Chars(tok.Location, [RangeSet().AddRange(ch, ch)]), tok.Next)
- | null => (RegularRule.Sequence([]), null)
- | _ => (null, tok)
- }
- }
-}
View
56 Nemerle.Parser.Macro/GrammarParser/ParseZeroOrMany.n
@@ -1,56 +0,0 @@
-using System;
-using Nemerle;
-using Nemerle.Collections;
-using Nemerle.Text;
-using Nemerle.Utility;
-using Nemerle.Compiler;
-using Nemerle.Compiler.Parsetree;
-using Nemerle.Compiler.Parsetree.PExpr;
-using Nemerle.Compiler.Token;
-
-namespace Nemerle.Parser
-{
- internal partial class GrammarParser
- {
- private ParseZeroOrMany[RuleT](
- tok : Token,
- parseDelemiter : Token -> Token,
- parseParser : Token -> RuleT * Token
- ) : list[RuleT] * Token
- where RuleT : class
- {
- def tok1 = match (tok)
- {
- | LooseGroup(child) => child
- | _ => tok
- };
-
- def (seq, nextTok) = parseParser(tok1);
-
- def loop(tok, acc) : list[RuleT] * Token
- {
- def nextTok1 = if (parseDelemiter == null) tok else parseDelemiter(tok);
-
- if (nextTok1 == null)
- (acc, tok) // predicate not matched
- else
- {
- def (seq, nextTok2) = parseParser(nextTok1);
- if (seq == null) (acc, nextTok2)
- else if (nextTok2 == null) (seq :: acc, null)
- else loop(nextTok2, seq :: acc)
- }
- }
-
- if (seq == null)
- ([], tok)
- else if (nextTok == null)
- ([seq], nextTok)
- else
- {
- def (res, nextTok) = loop(nextTok, [seq]);
- (res.Rev(), nextTok)
- }
- }
- }
-}
View
84 Nemerle.Parser.Macro/Macro.n
@@ -1,84 +0,0 @@
-using System;
-
-using Nemerle;
-using Nemerle.Collections;
-using Nemerle.Text;
-using Nemerle.Utility;
-using Nemerle.Compiler;
-using Nemerle.Compiler.Parsetree;
-using Nemerle.Compiler.Parsetree.PExpr;
-using Nemerle.Compiler.Token;
-using Nemerle.Imperative;
-
-namespace Nemerle.Parser
-{
- macro GrammarScope(_rules : Token)
- syntax ("parsergrammar", _rules)
- {
- <[ () ]>
- }
-
- [Nemerle.MacroUsage(Nemerle.MacroPhase.BeforeTypedMembers, Nemerle.MacroTargets.Class)]
- macro ParserGrammar(tb : TypeBuilder, options, rules)
- {
- GrammarImpl.BeforeTypedMembers(Macros.ImplicitCTX(), tb, options, rules);
- }
-
- [Nemerle.MacroUsage(Nemerle.MacroPhase.WithTypedMembers, Nemerle.MacroTargets.Class)]
- macro ParserGrammar(tb : TypeBuilder, _options, _rules)
- {
- GrammarImpl.WithTypedMembers(tb);
- }
-
- internal module GrammarImpl
- {
- public BeforeTypedMembers(typer : Typer, tb : TypeBuilder, options : PExpr, rules : PExpr) : void
- {
- try
- {
- def gc = GrammarCompiler(typer.Manager, typer.Env, tb, GrammarParser(typer, false).ParseGrammar(options, rules));
- tb.UserData[typeof(GrammarCompiler)] = gc;
- }
- catch
- {
- | e is GrammarException =>
- def loc = if (e.Location != Location.Default) e.Location else tb.Location;
- Message.Error(loc, e.Message);
- | _ is CriticalUnexpectedException => ()
- | _ is Nemerle.Compiler.Recovery => ()
- | e is Exception =>
- assert2(false, e.ToString());
- Message.Error(tb.Location, $"Exception $e");
- }
- }
-
- public WithTypedMembers(tb : TypeBuilder) : void
- {
- try
- {
- def compiler = tb.UserData[typeof(GrammarCompiler)] :> GrammarCompiler;
- when (compiler == null)
- Message.FatalError(tb.Location, "Grammar not parsed.");
-
- if (compiler.Manager.IsIntelliSenseMode)
- IntelliSenseSupport.AddIntelliSenseSupport(compiler);
- else
- when (compiler.AllRules)
- compiler.CompileGrammar();
-
- compiler.DefineAllMethods();
- }
- catch
- {
- | e is GrammarException =>
- def loc = if (e.Location != Location.Default) e.Location else tb.Location;
- Message.Error(loc, e.Message);
- | _ is CriticalUnexpectedException => ()
- | _ is Nemerle.Compiler.Recovery => ()
- | e is Exception =>
- assert2(false, e.ToString());
- Message.Error(tb.Location, $"Exception $e");
- }
- }
- }
-}
View
2 Nemerle.Parser.Macro/N2Source.n
@@ -112,7 +112,6 @@ namespace Nemerle.Parser
| e is GrammarException =>
def loc = if (e.Location != Location.Default) e.Location else sm.TypeBuilder.Location;
Message.Error(loc, e.Message);
- | _ is CriticalUnexpectedException => ()
| _ is Nemerle.Compiler.Recovery => ()
| e is Exception =>
assert2(false, e.ToString());
@@ -142,7 +141,6 @@ namespace Nemerle.Parser
| e is GrammarException =>
def loc = if (e.Location != Location.Default) e.Location else sm.TypeBuilder.Location;
Message.Error(loc, e.Message);
- | _ is CriticalUnexpectedException => ()
| _ is Nemerle.Compiler.Recovery => ()
| e is Exception =>
assert2(false, e.ToString());
View
43 Nemerle.Parser.Macro/Nemerle.Parser.Macro.nproj
@@ -255,51 +255,9 @@
<Compile Include="FSM\FSMBuilder.n">
<SubType>Code</SubType>
</Compile>
- <Compile Include="GrammarParser\Constructor.n">
- <SubType>Code</SubType>
- </Compile>
- <Compile Include="GrammarParser\ErrorReport.n">
- <SubType>Code</SubType>
- </Compile>
- <Compile Include="GrammarParser\ParseAttribute.n">
- <SubType>Code</SubType>
- </Compile>
- <Compile Include="GrammarParser\ParseCardinalityRule.n">
- <SubType>Code</SubType>
- </Compile>
- <Compile Include="GrammarParser\ParseGrammar.n">
- <SubType>Code</SubType>
- </Compile>
- <Compile Include="GrammarParser\ParseOneOrMany.n">
- <SubType>Code</SubType>
- </Compile>
- <Compile Include="GrammarParser\ParseChoice.n">
- <SubType>Code</SubType>
- </Compile>
- <Compile Include="GrammarParser\ParsePredicateRule.n">
- <SubType>Code</SubType>
- </Compile>
- <Compile Include="GrammarParser\ParseRange.n">
- <SubType>Code</SubType>
- </Compile>
- <Compile Include="GrammarParser\ParseRule.n">
- <SubType>Code</SubType>
- </Compile>
- <Compile Include="GrammarParser\ParseSequence.n">
- <SubType>Code</SubType>
- </Compile>
- <Compile Include="GrammarParser\ParseSimpleRule.n">
- <SubType>Code</SubType>
- </Compile>
- <Compile Include="GrammarParser\ParseZeroOrMany.n">
- <SubType>Code</SubType>
- </Compile>
<Compile Include="IntelliSenseSupport.n">
<SubType>Code</SubType>
</Compile>
- <Compile Include="Macro.n">
- <SubType>Code</SubType>
- </Compile>
<Compile Include="N2Source.n">
<SubType>Code</SubType>
</Compile>
@@ -317,7 +275,6 @@
<Folder Include="Compiler\GrammarCompiler\Optimization" />
<Folder Include="Compiler\RuleCompiler\" />
<Folder Include="FSM\" />
- <Folder Include="GrammarParser" />
<Folder Include="Properties\" />
</ItemGroup>
<ItemGroup>

0 comments on commit 9b6425c

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