public static Parser CreateParser(Terminal terminal, string terminator = "end") { var grammar = new TerminalTestGrammar(terminal, terminator); var parser = new Parser(grammar); CheckGrammarErrors(parser); return parser; }
public GrammarExL514() { NonTerminal A = new NonTerminal("A"); Terminal a = new Terminal("a"); A.Rule = "(" + A + ")" | a; this.Root = A; }//method
public FreeTextLiteralTestGrammar(Terminal terminal) : base(caseSensitive: true) { var rule = new BnfExpression(terminal); MarkReservedWords(Terminator); rule += Terminator; base.Root = new NonTerminal("Root"); Root.Rule = rule; }
public TerminalTestGrammar(Terminal terminal, string terminator = null) : base(caseSensitive: true) { Terminator = terminator; var rule = new BnfExpression(terminal); if (Terminator != null) { MarkReservedWords(Terminator); rule += Terminator; } base.Root = new NonTerminal("Root"); Root.Rule = rule; }
public GrammarEx434() { NonTerminal E = new NonTerminal("E"); NonTerminal T = new NonTerminal("T"); NonTerminal F = new NonTerminal("F"); Terminal id = new Terminal("id"); E.Rule = E + "+" + T | T; T.Rule = T + "*" + F | F; F.Rule = "(" + E + ")" | id; this.Root = E; }
public override void Init(GrammarData grammarData) { base.Init(grammarData); // convert strings to terminals, if needed _firstTerminal = _firstTerminal ?? Grammar.ToTerm(_firstString); if (_beforeStrings.Count > 0) { //SL pukes here, it does not support co/contravariance in full, we have to do it long way foreach (var s in _beforeStrings) _beforeTerminals.Add(Grammar.ToTerm(s)); } //Build description var beforeTerms = string.Join(" ", _beforeTerminals.Select(t => t.Name)); _description = string.Format("{0} if {1} comes before {2}.", _actionType, _firstTerminal.Name, beforeTerms); }
Token MatchCodeTerminal(Terminal terminal, ParsingContext context, ISourceStream source) { var remaining = source.Text.Substring(source.Location.Position); var stack = 1; var token = ""; while (stack > 0) { var index = remaining.IndexOf('}') + 1; var cut = remaining.Substring(0, index); token += cut; remaining = remaining.Substring(index); stack += cut.Count(x => x == '{'); stack--; } token = token.Substring(0, token.Length - 1); source.PreviewPosition += token.Length; return source.CreateToken(terminal.OutputTerminal); }
private void AddTerminal(TokenType type, Terminal term) { if (term == Grammar.Empty || term == Grammar.Eof || term == Grammar.SyntaxError) return; var tokenInfo = (TokenInfo)term.AstNodeConfig; if (tokenInfo == null) { Errors.Add(GrammarErrorLevel.Error, null, "Terminal {0} is doesn't have associated TokenInfo", term.Name); } else if(tokenInfo.TokenCategory == TokenCategory.Typename || tokenInfo.TokenCategory == TokenCategory.Keyword) { var keyMap = (tokenInfo.IsCaseInsensitive) ? caseInsensitiveKeywordToTerminal : keywordToTerminal; if (!keyMap.ContainsKey(term.Name)) keyMap.Add(term.Name, term); } else { SymbolToToken[(int)type] = term; } }
}//method #endregion #region VS Integration methods //Use this method for VS integration; VS language package requires scanner that returns tokens one-by-one. // Start and End positions required by this scanner may be derived from Token : // start=token.Location.Position; end=start + token.Length; public Token VsReadToken(ref int state) { Context.VsLineScanState.Value = state; if (Context.SourceStream.EOF()) { return(null); } if (state == 0) { NextToken(); } else { Terminal term = Data.MultilineTerminals[Context.VsLineScanState.TerminalIndex - 1]; Context.CurrentToken = term.TryMatch(Context, Context.SourceStream); } //set state value from context state = Context.VsLineScanState.Value; if (Context.CurrentToken != null && Context.CurrentToken.Terminal == _grammar.Eof) { return(null); } return(Context.CurrentToken); }
public MultiToken(Terminal term, SourceLocation location, TokenList childTokens) : base(term, location, string.Empty, null) { ChildTokens = childTokens; }
public void SetTerminal(Terminal terminal) { Terminal = terminal; this.EditorInfo = Terminal.EditorInfo; //set to term's EditorInfo by default }
protected TokenPreviewHint ShiftIf(Terminal thisSymbol, params Terminal[] comesBefore) { return(new TokenPreviewHint(PreferredActionType.Shift, thisSymbol, comesBefore)); }
public override string ToString() { return(Terminal.TokenToString(this)); } //method
Token Match_compile_end_tag(Terminal terminal, ParsingContext context, ISourceStream source) { if (source.MatchSymbol("__End")) { var p = source.Location.Position; var lineEnd = source.Text.IndexOf("\n", p); var text = source.Text.Substring(p, lineEnd - p + 1); return new Token(_comment, source.Location, text, text); } //otherwise return null return null; }
public Token CreateToken(Terminal terminal) { var tokenText = GetPreviewText(); return new Token(terminal, Location, tokenText, tokenText); }
public PowerShellGrammar() { InitializeTerminalFields(); InitializeNonTerminalFields(); InitializeComments(); // delegate to a new method, so we don't accidentally overwrite a readonly field. BuildProductionRules(); // there are special terms that can follow after skippable newlines. These terms are the // param block, elseif and else blocks. To make sure they aren't interpreted as statements, // we need to extra check when a newline occurs if the newline should be skipped _terms_after_newline = ( from t in new KeyTerm[] { @param, @else, @elseif } select new Tuple<KeyTerm, Regex>(t, new Regex(new_line_character.Pattern + "*" + t.Text)) ).ToArray(); // Skip newlines within statements, e.g.: // if ($true) # not the end of the statement - keep parsing! // { // "hi" // } this.skipped_new_line = new SkippedTerminal(this.new_line_character); new_line_character.ValidateToken += delegate(object sender, ValidateTokenEventArgs validateTokenEventArgs) { if (!IsNewlineExpected(validateTokenEventArgs)) { validateTokenEventArgs.ReplaceToken(this.skipped_new_line); } }; NonGrammarTerminals.Add(new BacktickLineContinuationTerminal()); Root = this.script_block; }
public void SetOutputTerminal(Grammar grammar, Terminal outputTerminal) { OutputTerminal = outputTerminal; grammar.NonGrammarTerminals.Add(this); }
public Token CreateToken(Terminal terminal, object value) { var tokenText = GetPreviewText(); return new Token(terminal, this.Location, tokenText, value); }
public PowerShellGrammar() { InitializeTerminalFields(); InitializeNonTerminalFields(); InitializeComments(); // delegate to a new method, so we don't accidentally overwrite a readonly field. BuildProductionRules(); // Skip newlines within statements, e.g.: // if ($true) # not the end of the statement - keep parsing! // { // "hi" // } this.skipped_new_line = new SkippedTerminal(this.new_line_character); new_line_character.ValidateToken += delegate(object sender, ValidateTokenEventArgs validateTokenEventArgs) { if (!IsNewlineExpected(validateTokenEventArgs)) { var current = validateTokenEventArgs.Context.CurrentToken; validateTokenEventArgs.ReplaceToken(this.skipped_new_line); } }; NonGrammarTerminals.Add(new BacktickLineContinuationTerminal()); Root = this.script_block; }
public TokenPreviewHint(PreferredActionType actionType, Terminal thisTerm, params Terminal[] comesBefore) { _actionType = actionType; _firstTerminal = thisTerm; _beforeTerminals.UnionWith(comesBefore); }
protected void SetTerminal(Terminal term) { _terminal = term; _terminal.Init(_language.GrammarData); }
public PieGrammar() { // Module is the root of it all var root = new NonTerminal("root"); this.Root = root; // Containers for various sets of related rules. Keywords = new Rules.KeywordRules(this); Operators = new Rules.OperatorRules(this); Literals = new Rules.LiteralRules(this); Namespaces = new Rules.NamespaceRules(this); Classes = new ClassRules(this); MethodDeclarations = new Rules.MethodDeclarationRules(this); Lists = new ListRules(this); IfBlocks = new IfBlockRules(this); Enums = new EnumRules(this); ForLoops = new ForLoopRules(this); WhileLoops = new WhileLoopRules(this); Constructors = new ConstructorRules(this); Properties = new PropertyRules(this); Exceptions = new ExceptionRules(this); Switches = new SwitchRules(this); Delegates = new DelegateRules(this); Interfaces = new InterfaceRules(this); // A type name, which my be simple, generic, or an array. TypeName = new NonTerminal("type_name"); // Use to signify an identifer: don't create others, just use this one. Identifier = new IdentifierTerminal("identifier"); Identifier.AllChars += '.'; // An identifier with generic type names "List{int}" GenericIdentifier = new NonTerminal("generic_identifier"); GenericIdentifier.Rule = Identifier + ToTerm("{") + Lists.PlusIdentifierList + ToTerm("}"); // A typename and an identifier. This needs to be in a unique rule // or the parser gets confused between variable declarations and method declarations. TwoIdentifiers = new NonTerminal("twoidentifiers"); TwoIdentifiers.Rule = TypeName + Identifier; // An expression that can be assigned a value. Assignable = new NonTerminal("Assignable"); // Explicit variable declaration: "int i" ExplicitVariableDeclaration = new NonTerminal("explicit_variable_declaration"); // A method invocation MethodInvocation = new NonTerminal("method_invocation"); // A method invocation with generic type names. GenericMethodInvocation = new NonTerminal("generic_method_invocation"); // Expression: pretty much anything found in a method body. expression = new NonTerminal("expression"); // Implicit variable declaration: "var i = 0" ImplicitVariableDeclaration = new NonTerminal("implicit_variable_declaration"); // List of valid conditional expressions ValidConditionals = new NonTerminal("valid_conditionals"); // new foo() Instantiation = new NonTerminal("instantiation"); // The list of valid arguments for a method invocation var validArguments = new NonTerminal("valid_arguments"); // List of arguments for a method invocation ArgumentList = new NonTerminal("argument_list"); // A return expression that does not return a value. Return = new NonTerminal("return"); // Return expression that does return a value. ReturnValue = new NonTerminal("return_value"); OpenParenthese = ToTerm("("); CloseParenthese = ToTerm(")"); // A continue expression for continuing loops. var Continue = ToTerm("continue", "continue_keyword"); Continue.Flags = TermFlags.IsReservedWord; // List of valid conditional expressions (for now, any expression). ValidConditionals.Rule = expression; // A directioned method argument "ref int i" or "out int i" var outArgument = new NonTerminal("out_argument"); outArgument.Rule = (Keywords.Out | Keywords.Ref) + expression; // Comma separated list of method invocation arguments. ArgumentList.Rule = MakeStarRule(ArgumentList, ToTerm(","), validArguments); // List of valid method invocation arguments. validArguments.Rule = expression | outArgument; // An array: identifier [] var array = new NonTerminal("array"); array.Rule = Identifier +ToTerm("[")+ ToTerm("]"); // Identifier that is indexed: identifier[1] var indexedIdentifier = new NonTerminal("indexed_identifier"); indexedIdentifier.Rule = Identifier + ToTerm("[") +expression+ ToTerm("]"); // A type name can be a simple name, an array, or a generic. TypeName.Rule = Identifier | array | GenericIdentifier; // int i var simpleExplicitVariableDeclaration = new NonTerminal("simple_explicit_variable_declaration"); simpleExplicitVariableDeclaration.Rule = Identifier + Identifier; // int [] i var arrayExplicitVariableDeclaration = new NonTerminal("array_explicit_variable_declaration"); arrayExplicitVariableDeclaration.Rule = Identifier + ToTerm("[") + ToTerm("]") + Identifier; // List{int} i var genericExplicitVariableDeclaration = new NonTerminal("generic_explicit_variable_declaration"); genericExplicitVariableDeclaration.Rule = Identifier + ToTerm("{") + Lists.StarIdentifierList + ToTerm("}") + Identifier; // var i ImplicitVariableDeclaration.Rule = Keywords.Var + Identifier; ExplicitVariableDeclaration.Rule = (simpleExplicitVariableDeclaration | arrayExplicitVariableDeclaration | genericExplicitVariableDeclaration | ImplicitVariableDeclaration ) + (Empty | Eos); // methodname(arguments) MethodInvocation.Rule = Identifier + OpenParenthese + ArgumentList + CloseParenthese + (Eos | Empty); GenericMethodInvocation.Rule = Identifier + ToTerm("{") + Lists.StarIdentifierList + ToTerm("}") + OpenParenthese + ArgumentList + CloseParenthese + (Eos | Empty); // Expressions that are "terminals" they don't go further, such as literals. var terms = new NonTerminal("terminals"); var parExp = new NonTerminal("par_exp"); expression.Rule = terms | Operators.Assignment | Operators.BinaryOperator | Operators.UnaryOperator | IfBlocks.SimpleIfBlock | IfBlocks.BodiedIfBlock | ForLoops.SimpleForLoop | ForLoops.BodiedForLoop | WhileLoops.SimpleWhileLoop | WhileLoops.BodiedWhileLoop | Delegates.EventMember | Return | ReturnValue | Instantiation | indexedIdentifier | Exceptions.ExceptionHandlingBlock | Exceptions.ThrowStatement | Switches.SwitchBlock; terms.Rule = Identifier | Literals.NumberLiteral | Literals.StringLiteral | Literals.BoolLiteral | Literals.Null | Literals.CharLiteral | ExplicitVariableDeclaration | MethodInvocation | GenericMethodInvocation | Keywords.Value | parExp; MarkTransient(terms); // An expression can be an expression nested in parenthese. parExp.Rule = ToTerm("(") + expression + ToTerm(")"); // Explicit variable declarations and identifiers can be assigned to. Assignable.Rule = ExplicitVariableDeclaration | Identifier; // continue to keep running a loop var continueKeyword = ToTerm("continue", "continue_keyword"); continueKeyword.Flags = TermFlags.IsReservedWord; // Return keyword can simply return, or return a value. var returnKeyword = ToTerm("return", "return_keyword"); returnKeyword.Flags = TermFlags.IsReservedWord; Return.Rule = returnKeyword + (Eos | Empty); ReturnValue.Rule = returnKeyword + expression + (Eos | Empty); // new foo() or new foo{int}() var newInstantiate = new NonTerminal("new_instantiate"); newInstantiate.Rule = Identifier + Classes.GenericTypeListOpt + OpenParenthese + ArgumentList + CloseParenthese; // new foo[2] var arrayInstantiate = new NonTerminal("array_instantiate"); arrayInstantiate.Rule = Identifier + ToTerm("[") + expression + ToTerm("]"); // The different ways of instantiating. Instantiation.Rule = Keywords.New + (newInstantiate | arrayInstantiate) + (Empty | Eos); Namespaces.Define(); Classes.Define(); MethodDeclarations.Define(); Lists.Define(); Operators.Define(); IfBlocks.Define(); Enums.Define(); ForLoops.Define(); WhileLoops.Define(); Constructors.Define(); Properties.Define(); Exceptions.Define(); Switches.Define(); Delegates.Define(); Interfaces.Define(); // Comments defining comment tokens. var singleLineComment = new CommentTerminal("SingleLineComment", "//", "\r", "\n", "\u2085", "\u2028", "\u2029"); var delimitedComment = new CommentTerminal("DelimitedComment", "/*", "*/"); NonGrammarTerminals.Add(singleLineComment); NonGrammarTerminals.Add(delimitedComment); NonGrammarTerminals.Add(ToTerm(@"\")); RegisterBracePair("(", ")"); RegisterBracePair("{", "}"); AddToNoReportGroup("("); AddToNoReportGroup(Eos); MarkPunctuation(OpenParenthese, CloseParenthese, ToTerm(","), ToTerm(":")); root.Rule = Namespaces.NamespaceMembersList; this.LanguageFlags = LanguageFlags.NewLineBeforeEOF | LanguageFlags.SupportsBigInt; }
public TokenPreviewHint(ParserActionType action, Terminal first) : this(action) { FirstTerminal = first; }
private void PushOutlineToken(Terminal term, SourceLocation location) { OutputTokens.Push(new Token(term, location, string.Empty, null)); }
protected TokenPreviewHint ShiftIf(Terminal thisSymbol, params Terminal[] comesBefore) { return new TokenPreviewHint(PreferredActionType.Shift, thisSymbol, comesBefore); }
public KinectitudeGrammar() { Number.DefaultFloatType = TypeCode.Double; Number.DefaultIntTypes = new TypeCode [] { TypeCode.Int64 }; CommentTerminal lineComment = new CommentTerminal("Line Comment", "//", "\r\n", "\n"); CommentTerminal blockComment = new CommentTerminal("Block Comment", "/*", "*/"); NonGrammarTerminals.Add(lineComment); NonGrammarTerminals.Add(blockComment); #region constants Constants.Add("true", ConstantReader.TrueValue); Constants.Add("false",ConstantReader.FalseValue); Constants.Add("True", ConstantReader.TrueValue); Constants.Add("False", ConstantReader.FalseValue); Constants.Add("Pi", new ConstantReader(Math.PI)); Constants.Add("E", new ConstantReader(Math.E)); #endregion Constants.Priority = Identifier.Priority + 1; #region operator terminals Terminal openBrace = ToTerm("{"); Terminal closeBrace = ToTerm("}"); Terminal colon = ToTerm(":"); Terminal comma = ToTerm(","); Terminal openBrac = ToTerm("("); Terminal closeBrac = ToTerm(")"); Becomes = ToTerm("=", "Becomes"); Eql = ToTerm("==", "Eql"); Lt = ToTerm("<", "Lt"); Gt = ToTerm(">", "Gt"); Le = ToTerm("<=", "Le"); Ge = ToTerm(">=", "Ge"); Neq = ToTerm("!=", "ne"); Plus = ToTerm("+", "Plus"); Minus = ToTerm("-", "Minus"); Mult = ToTerm("*", "Mult"); Div = ToTerm("/", "Div"); Rem = ToTerm("%", "Rem"); Pow = ToTerm("**", "Pow"); And = new RegexBasedTerminal("And", @"(&&)|(and)"); Or = new RegexBasedTerminal("Or", @"(\|\|)|(or)"); Not = new RegexBasedTerminal("Not", @"!|(not)"); LeftShift = ToTerm("<<", "leftShitf"); RightShift = ToTerm(">>", "RightShift"); BPlus = ToTerm("+=", "Plus Equals"); BMinus = ToTerm("-=", "Minus Equals"); BMult = ToTerm("*=", "Mult Equals"); BDiv = ToTerm("/=", "Div Equals"); BRem = ToTerm("%=", "Rem Equals"); BPow = ToTerm("^=", "Pow Equals"); BAnd = ToTerm("&=", "And Equals"); BOr = ToTerm("|=", "Or Equals"); BLeftShift = ToTerm("<<=", "LShift Equals"); BRightShift = ToTerm(">>=", "RShift Equals"); NonTerminal becomesExpr = new NonTerminal("Becomes expr"); becomesExpr.Rule = BPlus | BMinus | BDiv | BMult | BRem | BPow | BAnd | BOr | BLeftShift | BRightShift | Becomes; OpLookup[Plus] = OpLookup[BPlus] = OpCode.Plus; OpLookup[Minus] = OpLookup[BMinus] = OpCode.Minus; OpLookup[Div] = OpLookup[BDiv] = OpCode.Div; OpLookup[Mult] = OpLookup[BMult] = OpCode.Mult; OpLookup[Rem] = OpLookup[BRem] = OpCode.Rem; OpLookup[Pow] = OpLookup[BPow] = OpCode.Pow; OpLookup[And] = OpLookup[BAnd] = OpCode.And; OpLookup[Or] = OpLookup[BOr] = OpCode.Or; OpLookup[LeftShift] = OpLookup[BLeftShift] = OpCode.LeftShift; OpLookup[RightShift] = OpLookup[BRightShift] = OpCode.RightShift; OpLookup[Becomes] = OpCode.Becomes; OpLookup[Lt] = OpCode.Lt; OpLookup[Le] = OpCode.Le; OpLookup[Gt] = OpCode.Gt; OpLookup[Ge] = OpCode.Ge; OpLookup[Neq] = OpCode.Neq; OpLookup[Eql] = OpCode.Eql; #endregion #region values //NonTerminal value = new NonTerminal("value"); NonTerminal exactValue = new NonTerminal("exactValue", "exactValue"); #endregion #region value rules ThreeVal.Rule = Identifier + "." + Identifier + "." + Identifier; TwoVal.Rule = Identifier + "." + Identifier; IsType.Rule = "$" + Identifier; RegisterBracePair("(", ")"); RegisterBracePair("{", "}"); IsExactType.Rule = "#" + Identifier; ParentVal.Rule = "^" + Identifier + "." + Identifier | "^" + Identifier + "." + Identifier + "." + Identifier; exactValue.Rule = Identifier | TwoVal | ThreeVal | ParentVal; TypeMatcher.Rule = IsType | IsExactType| IsType + Plus + TypeMatcher | IsExactType + Plus + TypeMatcher; Argument.Rule = Expr | Expr + "," + Argument; Function.Rule = Identifier + ToTerm("(") +")" | Identifier + "(" + Argument + ")"; #endregion #region expressions NonTerminal term = new NonTerminal("term"); Expr.Rule = Expr + BinOp + Expr | UniOp + Expr | term; BinOp.Rule = Plus | Minus | Div | Mult | Rem | Pow | And | Or | Eql | Neq | Gt | Ge | Lt | Le | LeftShift | RightShift; UniOp.Rule = Not | Minus; term.Rule = Number | Str | openBrac + Expr + closeBrac | exactValue | Constants | Function; #endregion #region operator precedence /* NOTE: Order is taken from C++/C# order with power added in. * Higher number = more important * Increments are by 10 to allow easy adding of new terms * Power is not in C++/C#, but has been added where is seems to fit */ RegisterOperators(10, Associativity.Right, Becomes); RegisterOperators(20, Associativity.Left, Or); RegisterOperators(30, Associativity.Left, And); RegisterOperators(40, Associativity.Left, Eql, Neq); RegisterOperators(50, Associativity.Left, Eql, Neq); RegisterOperators(60, Associativity.Left, Ge, Le, Lt, Gt); RegisterOperators(70, Associativity.Left, LeftShift, RightShift); RegisterOperators(70, Associativity.Left, Plus, Minus); RegisterOperators(80, Associativity.Left, Div, Mult, Rem); RegisterOperators(90, Associativity.Left, Pow); RegisterOperators(100, Associativity.Left, Not); #endregion #region game creation rules NonTerminal IsPrototype = new NonTerminal("IsPrototype", "IsPrototype"); value.Rule = TypeMatcher | Expr; Names.Rule = Identifier + comma + Names | Identifier; IsPrototype.Rule = colon + Names | Empty; NonTerminal optionalProperties = new NonTerminal("optionalProperties", "optionalProperties"); optionalProperties.Rule = Properties | Empty; Properties.Rule = Identifier + Becomes + value | Identifier + Becomes + value + comma + optionalProperties; BasicDefinition.Rule = openBrac + optionalProperties + closeBrac; NonTerminal optionalComponent = new NonTerminal("optionalComponent", "optionalComponent"); optionalComponent.Rule = Component | Empty; Component.Rule = "Component" + Identifier + BasicDefinition + optionalComponent; NonTerminal optionalPrototype = new NonTerminal("optionalPrototype", "optionalPrototype"); optionalPrototype.Rule = Prototype | Empty; Prototype.Rule = "Prototype" + Identifier + EntityDefinition + optionalPrototype; NonTerminal optionalManager = new NonTerminal("optionalManager", "optionalManager"); optionalManager.Rule = Manager | Empty; Manager.Rule = "Manager" + Identifier + BasicDefinition + optionalManager; NonTerminal optionalService = new NonTerminal("optionalService", "optionalService"); optionalService.Rule = Service | Empty; Service.Rule = "Service" + Identifier + BasicDefinition + optionalService; Else.Rule = ToTerm("else") + "if" + openBrac + Expr + closeBrac + openBrace + Actions + closeBrace + Else | ToTerm("else") + "if" + openBrac + Expr + closeBrac + openBrace + Actions + closeBrace | "else" + openBrace + Actions + closeBrace + Else | "else" + openBrace + Actions + closeBrace; Condition.Rule = "if" + openBrac + Expr + closeBrac + openBrace + Actions + closeBrace + Else | "if" + openBrac + Expr + closeBrac + openBrace + Actions + closeBrace; NonTerminal optionalAssignment = new NonTerminal("optionalAssignment", "optionalAssignment"); optionalAssignment.Rule = Assignment | Empty; Loop.Rule = "while" + openBrac + Expr + closeBrac + openBrace + Actions + closeBrace | "for" + openBrac + optionalAssignment + ";" + Expr + ";" + Assignment + closeBrac + openBrace + Actions + closeBrace; NonTerminal optionalActions = new NonTerminal("OptionalActions", "OptionalActions"); Action.Rule = Identifier + BasicDefinition; Assignment.Rule = exactValue + becomesExpr + Expr; Actions.Rule = Condition + optionalActions | Action + optionalActions | Assignment + optionalActions | Loop + optionalActions; optionalActions.Rule = Actions | Empty; NonTerminal optionalEvt = new NonTerminal("optionalEvt", "optionalEvt"); optionalEvt.Rule = Evt | Empty; Evt.Rule = "Event" + Identifier + BasicDefinition + openBrace + optionalActions + closeBrace + optionalEvt; EntityDefinition.Rule = IsPrototype + BasicDefinition + openBrace + optionalComponent + optionalEvt + closeBrace; NonTerminal optionalEntity = new NonTerminal("optionalEntity", "optionalEntity"); optionalEntity.Rule = Entity | Empty; NonTerminal optionalIdentifier = new NonTerminal("optionalIdentifier", "optionalIdentifier"); optionalIdentifier.Rule = Identifier | Empty; Entity.Rule = "Entity" + optionalIdentifier + EntityDefinition + optionalEntity; NonTerminal optionalScene = new NonTerminal("optionalScene", "optionalScene"); optionalScene.Rule = Scene | Empty; Scene.Rule = "Scene" + Identifier + BasicDefinition + openBrace + optionalManager + optionalEntity + closeBrace + optionalScene; NonTerminal optionalDefinitions = new NonTerminal("optionalDefinitions", "optionalDefinitions"); optionalDefinitions.Rule = Definitions | Empty; Definitions.Rule = "define" + Identifier + "as" + ClassName + optionalDefinitions; NonTerminal optionalUses = new NonTerminal("optionalUses", "optionalUses"); optionalUses.Rule = Uses | Empty; Uses.Rule = "using" + ClassName + openBrace + Definitions + closeBrace + optionalUses; Game.Rule = optionalUses + "Game" + BasicDefinition + openBrace + optionalPrototype + optionalService + Scene + closeBrace; #endregion Root = Game; //Removes from the tree, we don't care about having these there MarkPunctuation("{", "}", "(", ")", ":", "$", "@", "#", "^", "Game", "using", "define", "Scene", "Entity", ",", "if", "Component", "Manager", "Prototype", "=", ".", "as", "Event", "else", "Service", ";", "while", "for"); MarkReservedWords("using", "define", "if", "else", "while", "for"); MarkTransient(BasicDefinition, value, IsPrototype, term, exactValue, optionalActions, optionalEvt, optionalComponent, optionalProperties, optionalManager, optionalPrototype, optionalService, optionalUses, optionalDefinitions, optionalEntity, optionalScene, optionalIdentifier, optionalAssignment, becomesExpr); }
// Remove this when it appears in Irony (if ever). See https://irony.codeplex.com/discussions/438247#post1026398 /// <summary> /// Replace the current token with a token based on <paramref name="newTerminal"/>, while keeping everything else /// the same as the current token. /// </summary> /// <param name="newTerminal"></param> public static void ReplaceToken(this ValidateTokenEventArgs @this, Terminal newTerminal) { @this.ReplaceToken(new Token(newTerminal, @this.Context.CurrentToken.Location, @this.Context.CurrentToken.Text, @this.Context.CurrentToken.Value)); }
}//class private Parser CreateParser(Terminal terminal) { var grammar = new FreeTextLiteralTestGrammar(terminal); return new Parser(grammar); }
public Token CreateToken(Terminal terminal, object value) { var tokenText = GetPreviewText(); return(new Token(terminal, this.Location, tokenText, value)); }
private void PushToken(Terminal term) { PushToken(new Token(term, currentLoc, string.Empty, null)); }
public SkippedTerminal(Terminal terminalToSkip) : base("skipped " + terminalToSkip.Name, TokenCategory.Outline, TermFlags.IsNonGrammar) { }
public ZodiacGrammar() { #region Lexical StringLiteral charLiteral = ZodiacTerminalFactory.CreateZodiacChar("charLiteral"); StringLiteral stringLiteral = ZodiacTerminalFactory.CreateZodiacString("stringLiteral"); NumberLiteral number = ZodiacTerminalFactory.CreateZodiacNumber("number"); IdentifierTerminal identifier = ZodiacTerminalFactory.CreateZodiacIdentifier("identifier"); CommentTerminal singleLineComment = new CommentTerminal("singleLineComment", "@@", "\r", "\n"); CommentTerminal mutiLineComment = new CommentTerminal("mutiLineComment", "@{", "}@"); NonGrammarTerminals.Add(singleLineComment); NonGrammarTerminals.Add(mutiLineComment); var comment = new NonTerminal("comment"); comment.Rule = singleLineComment | mutiLineComment; //Symbols KeyTerm colon = ToTerm(":", "colon"); KeyTerm semi = ToTerm(";", "semi"); KeyTerm dot = ToTerm(".", "dot"); KeyTerm comma = ToTerm(",", "comma"); KeyTerm cast = ToTerm("=>", "cast"); KeyTerm Lbr = ToTerm("{"); KeyTerm Rbr = ToTerm("}"); KeyTerm Lbra = ToTerm("["); KeyTerm Rbra = ToTerm("]"); KeyTerm Lpar = ToTerm("("); KeyTerm Rpar = ToTerm(")"); #endregion Lexical #region /* 2 Type declarations and definitions */ var required_type = new NonTerminal("required_type"); var simple_type = new NonTerminal("simple_type"); var list_type = new NonTerminal("list_type"); // var func_type = new NonTerminal("func_type"); //var type_declaration = new NonTerminal("type_declaration"); var type_definition = new NonTerminal("type_definition"); var structed_type = new NonTerminal("structed_type"); var basic_type = new NonTerminal("basic_type"); var type_identifier_list = new NonTerminal("type_identifier_list"); var type_member = new NonTerminal("type_member"); var member_variable = new NonTerminal("member_variable"); var member_function = new NonTerminal("member_function"); var member_family_opt = new NonTerminal("family option"); /* 3 Constructors and Convertor */ var constructor = new NonTerminal("constructor"); var default_constructor = new NonTerminal("default_constructor"); var required_type_default_constructor = new NonTerminal("required_type_default_constructor"); var structed_type_default_constructor = new NonTerminal("structed_type_default_constructor"); var structed_type_identifier = new NonTerminal("structed_type_identifier"); var defined_constructor = new NonTerminal("defined_constructor"); var required_type_defined_constructor = new NonTerminal("required_type_defined_constructor"); var structed_type_defined_constructor = new NonTerminal("structed_type_defined_constructor"); var simple_type_defined_constructor = new NonTerminal("simple_type_defined_constructor"); var list_type_defined_constructor = new NonTerminal("list_type_defined_constructor"); var range_parameter = new NonTerminal("range_parameter"); var range = new NonTerminal("range"); var indexed_parameter = new NonTerminal("indexed_parameter"); var fill_parameter = new NonTerminal("fill_parameter"); var index = new NonTerminal("index"); var converter = new NonTerminal("converter"); var inherit_converter = new NonTerminal("inherit_converter"); var ansc_converter = new NonTerminal("ansc_converter"); var desc_converter = new NonTerminal("desc_converter"); /* 4 Variables definitions */ var variable_definition = new NonTerminal("variable_definition"); var variable_default_definition = new NonTerminal("variable_default_definition"); var variable_access = new NonTerminal("variable_access"); var entire_variable = new NonTerminal("entire_variable"); var component_variable = new NonTerminal("component_variable"); var indexed_variable = new NonTerminal("indexed_variable"); var array_variable = new NonTerminal("array_variable"); var index_expression = new NonTerminal("index_expression"); var field_designator = new NonTerminal("field_designator"); var record_variable = new NonTerminal("record_variable"); var field_identifier = new NonTerminal("field_identifier"); var constructor_variable = new NonTerminal("constructor_variable"); var converter_variable = new NonTerminal("converter_variable"); /* 5 Function declarations and definitions */ // var function_declaration = new NonTerminal("function_declaration"); // var function_normal_declaration = new NonTerminal("normal_function_declaration"); // var function_operator_declaration = new NonTerminal("operator_function_declaration"); var function_option = new NonTerminal("function_option"); var function_definition = new NonTerminal("function_definition"); var function_normal_definition = new NonTerminal("normal_function_definition"); var function_operator_definition = new NonTerminal("operator_function_definition"); var return_type_definition = new NonTerminal("return_type_definition"); var return_type_list = new NonTerminal("return_type_list"); var function_body = new NonTerminal("function_body"); var function_parameter_block = new NonTerminal("function_parameter_block"); var function_parameter_list_opt = new NonTerminal("function_parameter_list_opt"); var function_parameter_list = new NonTerminal("function_parameter_list"); var function_parameter = new NonTerminal("function_parameter"); var function_parameter_default_list_opt = new NonTerminal("function_parameter_default_list_opt"); var function_parameter_default_list = new NonTerminal("function_parameter_default_list"); var function_parameter_default = new NonTerminal("function_parameter_default"); var function_instruction_block = new NonTerminal("function_instruction_block"); var function_scope_body = new NonTerminal("function_scope_body"); /* 6 operator */ var operators = new NonTerminal("operator"); //operator var unary_operator = new NonTerminal("unary_operator"); var pow_operator = new Terminal("pow_operator"); var multiply_operator = new Terminal("multiply_operator"); var add_operator = new Terminal("add_operator"); var shift_operator = new Terminal("shift_operator"); var compare_operator = new Terminal("compare_operator"); var equal_operator = new Terminal("equal_operator"); var bit_and_operator = new Terminal("bit_and_operator "); var bit_xor_operator = new Terminal("bit_xor_operator "); var bit_or_operator = new Terminal("bit_or_operator"); var and_operator = new Terminal("and_operator"); var or_operator = new Terminal("or_operator"); var bin_operator = new NonTerminal("bin_operator"); var assignment_operator = new NonTerminal("assignment_operator"); var assignment_value_operator = new NonTerminal("assignment_value_operator"); var assignment_reference_operator = new NonTerminal("assignment_reference_operator"); /* 7 Expressions */ var expression = new NonTerminal("expression"); var parenthesized_expression = new NonTerminal("parenthesized_expression"); var bin_op_expression = new NonTerminal("bin_op_expression"); var unary_expression = new NonTerminal("unary_expression"); var primary_expression = new NonTerminal("primary_expression"); var list_expression = new NonTerminal("list_expression"); // var list_subscript_expression = new NonTerminal("list_subscript_expression"); var list_normal_expression = new NonTerminal("list_normal_expression"); var list_select_expression = new NonTerminal("list_select_expression"); var list_string_expression = new NonTerminal("list_string_expression"); var term = new NonTerminal("term");//TODO var literal = new NonTerminal("literal"); var expression_list = new NonTerminal("expression_list"); /* 8 Statements */ var statement = new NonTerminal("statement"); var simple_statement = new NonTerminal("simple_statement"); var variable_definition_statement = new NonTerminal("variable_definition_statement"); var assignment_statement = new NonTerminal("assignment_statement"); var access_statement = new NonTerminal("access_statement"); var structed_statement = new NonTerminal("structed_statement"); var if_statement = new NonTerminal("if_statement"); var else_part_opt = new NonTerminal("else_part_opt"); var else_part = new NonTerminal("else_part"); var while_statement = new NonTerminal("while_statement"); var for_statement = new NonTerminal("for_statement"); var break_statement = new NonTerminal("break_statement"); var continue_statement = new NonTerminal("continue_statement"); var ret_statement = new NonTerminal("ret_statement"); var return_statement = new NonTerminal("return_statement"); var escape_statement = new NonTerminal("escape_statement"); /* 9 Scope */ var scope = new NonTerminal("scope"); var scope_body = new NonTerminal("scope_body"); var scope_body_opt = new NonTerminal("scope_body_opt"); var statement_list = new NonTerminal("statement_list"); //var declaration = new NonTerminal("declaration"); var definition = new NonTerminal("definition"); var main_scope_body_opt = new NonTerminal("scope_body_opt"); var main_scope_body = new NonTerminal("scope_body"); var main_statement_list = new NonTerminal("statement_list"); var main_statement = new NonTerminal("statement"); /* 11 Programs */ var program = new NonTerminal("program"); var program_heading = new NonTerminal("program_heading"); var have_sequence_list = new NonTerminal("program_heading"); var have_sequence = new NonTerminal("program_heading"); #endregion NonTerminals RegisterOperators(-1, "=", ":="); RegisterOperators(1, "||"); RegisterOperators(2, "&&"); RegisterOperators(3, "|"); RegisterOperators(4, "^"); RegisterOperators(5, "&"); RegisterOperators(6, "==", "!="); RegisterOperators(7, "<", ">", "<=", ">="); RegisterOperators(8, "<<", ">>"); RegisterOperators(9, "+", "-"); RegisterOperators(10, "*", "/", "%"); RegisterOperators(11, Associativity.Right, "^^"); this.MarkPunctuation(";", ",", "(", ")", "{", "}", "[", "]", ":"); // this.MarkTransient(type_definition, statement, expression, bin_operator, expression); this.MarkTransient(statement, expression, bin_operator, expression); var identifier_ext = new NonTerminal("identifier_ext"); var identifier_list = new NonTerminal("identifier_list"); var member_access_list = new NonTerminal("member_access_list"); var member_access = new NonTerminal("member_access"); var member_access_with_segement = new NonTerminal("member_access"); var member_access_segments_opt = new NonTerminal("member_access_segments_opt"); var member_access_segment = new NonTerminal("member_access_segment"); var array_indexer = new NonTerminal("array_indexer"); var argument = new NonTerminal("argument"); var argument_list_par = new NonTerminal("argument_list_par"); var argument_list_par_opt = new NonTerminal("argument_list_par_opt"); var argument_list = new NonTerminal("argument_list"); var argument_list_opt = new NonTerminal("argument_list_opt"); /* Basic */ /* identifier */ identifier_ext.Rule = required_type; //?? identifier_list.Rule = MakePlusRule(identifier_list, comma, identifier); /* member_access*/ member_access_list.Rule = MakePlusRule(member_access, comma, member_access); member_access_with_segement.Rule = member_access + member_access_segment; member_access.Rule = identifier_ext | member_access_with_segement; member_access_segments_opt.Rule = member_access_segment | Empty;//MakeStarRule(member_access_segments_opt, null, member_access_segment); member_access_segment.Rule = (dot + identifier) | (cast + required_type) | array_indexer | argument_list_par; array_indexer.Rule = "[" + expression + "]"; /* arguments */ argument_list.Rule = expression; argument_list_par.Rule = Lpar + argument_list_opt + Rpar; argument_list_par_opt.Rule = Empty | argument_list_par; argument_list.Rule = MakePlusRule(argument_list, comma, expression); argument_list_opt.Rule = Empty | argument_list; /* Rule */ /* 2 Type declarations and definitions */ /* 2.1 Required-types */ required_type.Rule = identifier | simple_type | list_type /*| func_type*/; simple_type.Rule = ToTerm("long") | "real" | "bool" | "char" ; list_type.Rule = ToTerm("list"); // func_type.Rule = ToTerm("func"); /* 2.1 Type-declarations */ //type_declaration.Rule = ToTerm("type") + identifier + semi; /* 2.2 Type-definitions */ basic_type.Rule = (ToTerm("<-") + identifier) | Empty; type_definition.Rule = ToTerm("type") + identifier + basic_type + Lbr + structed_type + Rbr; //TODO //type_basic.Rule // type_identifier_list.Rule = MakePlusRule(type_identifier_list, comma, identifier); structed_type.Rule = MakeStarRule(structed_type, null, type_member); type_member.Rule = member_variable | member_function; member_variable.Rule = ToTerm("var") + identifier + assignment_reference_operator + required_type + semi; member_family_opt.Rule = ToTerm("family") | Empty; member_function.Rule = member_family_opt + function_definition; /* 3 Constructors and Converters */ constructor.Rule = defined_constructor | default_constructor; /* 3.2 Default-Constructors */ default_constructor.Rule = required_type_default_constructor | structed_type_default_constructor; required_type_default_constructor.Rule = required_type; structed_type_default_constructor.Rule = identifier; /* 3.3 Defined-Constructors */ defined_constructor.Rule = required_type_defined_constructor | structed_type_default_constructor; required_type_defined_constructor.Rule = simple_type_defined_constructor | list_type_defined_constructor; simple_type_defined_constructor.Rule = simple_type + Lpar + expression + Rbr; list_type_defined_constructor.Rule = list_type + (range_parameter | indexed_parameter); range_parameter.Rule = Lpar + range + comma + range_parameter + Rpar; range.Rule = expression; indexed_parameter.Rule = Lbra + index + comma + index + Rbra + (range_parameter | fill_parameter); fill_parameter.Rule = Lpar + expression + Rpar; index.Rule = expression; structed_type_defined_constructor.Rule = structed_type_identifier + Lpar + Rpar;//------ /* 3.4 Converter */ converter.Rule = constructor | inherit_converter; inherit_converter.Rule = ansc_converter | desc_converter; ansc_converter.Rule = ToTerm("ance"); desc_converter.Rule = ToTerm("desc"); /* 4 Variables definitions */ variable_definition.Rule = ToTerm("var") + identifier_list + assignment_operator + expression_list; variable_access.Rule = member_access; // converter_variable; /* 4.2 Entire-Variables */ // entire_variable.Rule = identifier; /* 4.3 Component-Variables */ //component_variable.Rule = indexed_variable | field_designator; //indexed_variable.Rule = array_variable + Lbra + index_expression + Rbra; //array_variable.Rule = variable_access; //index_expression.Rule = expression; //field_designator.Rule = record_variable + dot + field_identifier; //record_variable.Rule = variable_access; //field_identifier.Rule = identifier; /* 4.4 Field-designators */ //constructor_variable.Rule = constructor; //converter_variable.Rule = converter; /* 5 Function declarations and definition */ function_option.Rule = "static" | Empty; function_definition.Rule = function_option + (function_normal_definition | function_operator_definition) + return_type_definition + Lbr + function_body + Rbr; function_normal_definition.Rule = "func" + identifier; function_operator_definition.Rule = "oper" + bin_operator; return_type_definition.Rule = Empty | (":" + return_type_list); return_type_list.Rule = MakePlusRule(return_type_list, ToTerm(","), required_type); function_body.Rule = function_parameter_block + function_instruction_block; function_parameter_block.Rule = function_parameter_list | function_parameter_default_list | (function_parameter_list + function_parameter_default_list) | Empty; //function_parameter_list_opt.Rule = function_parameter_list | Empty; function_parameter_list.Rule = MakePlusRule(function_parameter_list, null, function_parameter); function_parameter.Rule = "param" + identifier_list + assignment_operator + required_type + semi; //function_parameter_default_list_opt.Rule = function_parameter_default_list | Empty; function_parameter_default_list.Rule = MakePlusRule(function_parameter_default_list, null, function_parameter_default); function_parameter_default.Rule = "param" + identifier_list + assignment_operator + required_type + argument_list_par + semi; function_instruction_block.Rule = function_scope_body | Empty; function_scope_body.Rule = scope_body; /* 6 operator */ operators.Rule = unary_operator | pow_operator | bin_operator; unary_operator.Rule = ToTerm("!") | "~" | "+" | "-"; bin_operator.Rule = ToTerm("*") | "/" | "%" | "+" | "-" | "<<" | ">>" | "<" | ">" | "==" | "&" | "^" | "|" | "&&" | "||" | "<=" | ">=" | "!="; assignment_value_operator.Rule = ToTerm(":="); assignment_reference_operator.Rule = ToTerm("="); assignment_operator.Rule = assignment_value_operator | assignment_reference_operator; /* 7 Expressions */ expression.Rule = primary_expression | bin_op_expression; parenthesized_expression.Rule = Lpar + expression + Rpar; bin_op_expression.Rule = expression + bin_operator + expression; unary_expression.Rule = unary_operator + primary_expression; list_expression.Rule = list_normal_expression | list_select_expression | list_string_expression; list_normal_expression.Rule = ToTerm("[") + expression_list + "]"; //list_subscript_expression.Rule = ToTerm("List") + "[" + expression + comma + expression + "]" + "(" + expression + ")"; list_select_expression.Rule = ToTerm("from") + identifier + "in" + expression + "where" + expression + ToTerm("select") + expression; list_string_expression.Rule = stringLiteral; primary_expression.Rule = literal | unary_expression | parenthesized_expression | member_access | list_expression; literal.Rule = number | charLiteral | "True" | "False" | "Null"; expression_list.Rule = MakePlusRule(expression_list, comma, expression); /* 8 Statements */ statement.Rule = scope | simple_statement | structed_statement | ret_statement | break_statement | continue_statement; /* 8.2 Simple-statements */ simple_statement.Rule = assignment_statement | variable_definition_statement | access_statement; variable_definition_statement.Rule = variable_definition + semi; assignment_statement.Rule = member_access_list + assignment_operator + expression_list + semi; access_statement.Rule = member_access + semi; /* 8.3 Structed-statements */ structed_statement.Rule = if_statement | while_statement | for_statement; if_statement.Rule = ToTerm("if") + Lpar + expression + Rpar + scope + else_part_opt; else_part_opt.Rule = else_part | Empty; else_part.Rule = ToTerm("else") + scope; while_statement.Rule = ToTerm("while") + Lpar + expression + Rpar + scope; for_statement.Rule = ToTerm("for") + identifier + "in" + member_access + scope; /* 8.4 Loop-statements */ break_statement.Rule = "break" + semi; continue_statement.Rule = "continue" + semi; /* 8.5 Ret-statements */ ret_statement.Rule = return_statement | escape_statement; return_statement.Rule = ToTerm("return") + argument_list_opt + semi; escape_statement.Rule = ToTerm("escape") + identifier_list + semi; main_statement.Rule = scope | simple_statement | structed_statement | definition; main_scope_body_opt.Rule = main_scope_body | Empty; main_scope_body.Rule = main_statement_list; main_statement_list.Rule = MakePlusRule(main_statement_list, null, main_statement); /* 9 Scope */ scope.Rule = Lbr + scope_body_opt + Rbr; scope_body_opt.Rule = scope_body | Empty; /* 9.2 Scope-bodys */ //scope_body.Rule = declaration | definition | statement | comment; scope_body.Rule = statement_list; statement_list.Rule = MakePlusRule(statement_list, null, statement); //declaration.Rule = function_declaration; definition.Rule = function_definition | type_definition; /* 11 Program */ program.Rule = program_heading + main_scope_body_opt; program_heading.Rule = have_sequence_list | Empty; have_sequence_list.Rule = MakePlusRule(have_sequence_list, comma, have_sequence); have_sequence.Rule = ToTerm("have") + identifier + ".d"; this.Root = program; #region Define_Keywords this.MarkReservedWords("long", "real", "char", "bool", "list", "func", "oper", "var", "param", "return", "escape", "type", "family", "static", "True" , "False", "if", "else", "for in", "while", "break", "continue", "from", "where", "select", "ance", "desc", "have", "Null"); #endregion }