public override void Init(ParsingContext context, ParseTreeNode treeNode) { base.Init(context, treeNode); ParseTreeNode declNode = treeNode.ChildNodes[1].FirstChild; if (declNode.ToString() == "TypedFunction") { declNode = declNode.FirstChild; } TypeToken = declNode.FirstChild.Token; Name = declNode.LastChild.Token; DeclNode = (ScopedNode)declNode.AstNode; Parameters = (ParametersNode)treeNode.ChildNodes[2].AstNode; if (TypeToken != null) { AsString = Label + TypeToken.ValueString + " " + Name.ValueString; } ChildNodes.Add(DeclNode); DeclNode.Parent = this; ChildNodes.Add(Parameters); Parameters.Parent = this; if (UoTypeToken == Types.List && !(this is UoCoreFunctionNode)) { context.AddParserMessage(ParserErrorLevel.Warning, new SourceSpan(TypeToken.Location, TypeToken.Length), "Function may not return a list."); } }
public override void Init(ParsingContext context, ParseTreeNode treeNode) { base.Init(context, treeNode); Body = (ScopedNode)treeNode.ChildNodes[3].AstNode; ChildNodes.Add(Body); DeclNode.Parent = Parameters.Parent = Body.Parent = this; // Add params to scope foreach (DeclarationNode node in Parameters.ChildNodes) { Field field = new Field(node, context); AddVar(field, context); } // Add vars in all child nodes to scope foreach (DeclarationNode node in FindNodesOfType <DeclarationNode>(treeNode.ChildNodes[3])) { Field field = new Field(node, context); AddVar(field, context); } List <ReturnNode> returns = FindNodesOfType <ReturnNode>(treeNode.ChildNodes[3], firstOnly: true); bool hasreturns = returns != null && returns.Count > 0; if (UoTypeToken != Types.Void && !hasreturns) { context.AddParserMessage(Irony.Parsing.ParserErrorLevel.Warning, this.Span, "A non-void function should return an expression."); } }
protected override void BuildExpression(ParsingContext context, ParseTreeNode treeNode) { m_Operation = (ScopedNode)treeNode.ChildNodes[1].FirstChild.AstNode; m_Operation.SetUoToken(((Tokenizer)Operation.TreeNode.Term).Tokenize()); AsString = m_Operation.Term.Name; ChildNodes.Add(Left = Reduce(treeNode.FirstChild)); ChildNodes.Add(Right = Reduce(treeNode.LastChild)); Left.Parent = Right.Parent = this; }
protected override void BuildExpression(ParsingContext context, ParseTreeNode treeNode) { AsString = "UnaryPrefixed"; m_Operation = (ScopedNode)treeNode.ChildNodes[0].FirstChild.AstNode; m_Operation.SetUoToken(((Tokenizer)Operation.TreeNode.Term).Tokenize()); ChildNodes.Add(Expression = ExpressionNode.Reduce(treeNode.LastChild)); ChildNodes[0].Parent = this; AsString = m_Operation.Term.Name; }
protected virtual void LoadNodes(ParsingContext context, ParseTreeNode treeNode) { ChanceNode = treeNode.ChildNodes[1].ChildNodes.Count > 0 ? (EventParamNode <UoToken.UoTypeName_int>)treeNode.ChildNodes[1].FirstChild.AstNode : null; ParseTreeNode declNode = treeNode.ChildNodes[2]; Name = declNode.FirstChild.Token; EventParameter = declNode.ChildNodes.Count > 1 ? (ScopedNode)declNode.LastChild.AstNode : null; DeclNode = (EventDecNode)declNode.AstNode; Parameters = (ParametersNode)treeNode.ChildNodes[3].AstNode; Body = (ScopedNode)treeNode.ChildNodes[4].AstNode; }
public override void Init(ParsingContext context, ParseTreeNode treeNode) { base.Init(context, treeNode); LanguageOption Options = context.GetOptions(); Expression = ExpressionNode.Reduce(treeNode.ChildNodes[1]); ChildNodes.Add(Expression); Expression.Parent = this; if (treeNode.ChildNodes.Count > 3) { Statement = StatementNode.GetStatement(treeNode.ChildNodes[2], context) as ScopedNode; if (Statement != null) { ChildNodes.Add(Statement); Statement.Parent = this; } } else { Statement = null; } if (!Options.HasOption(LanguageOption.UnBracedLoopsIfs) && !(Statement is BlockNode)) { context.AddParserMessage(ParserErrorLevel.Error, Statement == null ? treeNode.Span : Statement.Span, "Statement must be enclosed in a block."); } if (treeNode.LastChild.ChildNodes.Count > 0) { if (treeNode.LastChild.FirstChild.ChildNodes.Count > 1) { StatementElse = StatementNode.GetStatement(treeNode.LastChild.FirstChild.ChildNodes[1], context) as ScopedNode; if (StatementElse != null) { ChildNodes.Add(StatementElse); StatementElse.Parent = this; } } else { StatementElse = null; } if (!Options.HasOption(LanguageOption.UnBracedLoopsIfs) && !(StatementElse is BlockNode)) { context.AddParserMessage(ParserErrorLevel.Error, StatementElse == null ? treeNode.LastChild.FirstChild.Span : StatementElse.Span, "Statement must be enclosed in a block."); } } }
public override void Init(ParsingContext context, ParseTreeNode treeNode) { base.Init(context, treeNode); LanguageOption Options = context.GetOptions(); bool RequiresColon = Options.HasOption(LanguageOption.ColonAfterSwitchCase); ScopedNode labels = (ScopedNode)treeNode.FirstChild.AstNode; labels.Parent = this; ChildNodes.Add(labels); Labels = new List <ExpressionNode>(treeNode.FirstChild.ChildNodes.Count); foreach (ParseTreeNode node in treeNode.FirstChild.ChildNodes) { if (RequiresColon && context.Source.Text[node.Span.EndPosition - 1] != ':') { context.AddParserMessage(ParserErrorLevel.Error, node.FirstChild.Span, "Colon expected."); } if (((Tokenizer)node.ChildNodes[0].Term).Tokenize() == Keyword.Default) // kinda hacky { m_isDefault = true; } else { ExpressionNode exp = ExpressionNode.Reduce(node.ChildNodes[1]); Labels.Add(exp); labels.ChildNodes.Add(exp); exp.Parent = labels; if (exp.UoTypeToken == null || !exp.UoTypeToken.IsLiteral) { context.AddParserMessage(ParserErrorLevel.Error, exp.Span, "A case label must be a literal value."); } } } ScopedNode statements = (ScopedNode)treeNode.LastChild.AstNode; statements.Parent = this; ChildNodes.Add(statements); StatementNode.AddStatementList(statements, treeNode.LastChild, context); Statements = new List <IStatement>(statements.ChildNodes.Count); foreach (AstNode node in statements.ChildNodes) { if (node is IStatement) { Statements.Add((IStatement)node); } } AsString += " " + (isDefault ? "default" : "case"); }
public static void AddStatementList(ScopedNode aststatementsroot, ParseTreeNode parsestatementsroot, ParsingContext context, bool adderrors = true) { foreach (ParseTreeNode node in parsestatementsroot.ChildNodes) { ParseTreeNode cnode = null; if (node.AstNode is DeclarationNode) // member { cnode = node; } else if (node.ChildNodes.Count > 0) { if (node.FirstChild.AstNode is DeclarationNode) { cnode = node.FirstChild; } else if (node.FirstChild.ChildNodes.Count > 0 && node.FirstChild.FirstChild.AstNode is DeclarationNode) { cnode = node.FirstChild.FirstChild; } } if (cnode != null) { if (cnode.AstNode is MemberDeclarationNode) { UOSLBase.AddMember(new Field((MemberDeclarationNode)cnode.AstNode, context), context); } //else if this is a regular variable declaration within a function, the function will add the var to scope when the function node is created } ScopedNode newnode = GetStatement(node, context, adderrors); if (newnode != null) { aststatementsroot.ChildNodes.Add(newnode); newnode.Parent = aststatementsroot; } } }
public override void CheckScope(Irony.Parsing.ParsingContext context) { // find function ScopedNode parent = this.Parent as ScopedNode; while (parent != null && !(parent is FunctionDefNode)) { parent = parent.Parent as ScopedNode; } if (parent != null) { if (parent.UoTypeToken == Types.Void) { if (optExpression != null) { context.AddParserMessage(Irony.Parsing.ParserErrorLevel.Error, this.Span, "A void function may not return an expression."); } } else if (optExpression == null) { context.AddParserMessage(Irony.Parsing.ParserErrorLevel.Error, this.Span, "This function must return an expression of type {0}.", parent.UoTypeToken.Value); } else { if (parent.UoTypeToken != optExpression.UoTypeToken) { if (optExpression.UoTypeToken == null) { context.AddParserMessage(Irony.Parsing.ParserErrorLevel.Info, optExpression.Span, "Return type indeterminate must be of type {0} at runtime.", parent.UoTypeToken.Value); } else { context.AddParserMessage(Irony.Parsing.ParserErrorLevel.Error, optExpression.Span, "Return type must be of type {0}. This expression evaluates to {1}", parent.UoTypeToken.Value, optExpression.UoTypeToken.Value); } } } } }
public override void Init(ParsingContext context, ParseTreeNode treeNode) { base.Init(context, treeNode); LanguageOption Options = context.GetOptions(); ParseTreeNode forBlock = treeNode.ChildNodes[1].FirstChild; if (forBlock.ChildNodes[0].ChildNodes.Count > 0 && forBlock.ChildNodes[0].FirstChild.ChildNodes.Count > 0) { LoopInitializer = StatementNode.GetStatement(forBlock.ChildNodes[0].FirstChild.FirstChild, context) as IStatement; if (LoopInitializer != null) { ((ScopedNode)LoopInitializer).Parent = this; ChildNodes.Add(((ScopedNode)LoopInitializer)); } } if (forBlock.ChildNodes[1].ChildNodes.Count > 0 && forBlock.ChildNodes[1].FirstChild.ChildNodes.Count > 0) { LoopExpression = ExpressionNode.Reduce(forBlock.ChildNodes[1].FirstChild.FirstChild); if (LoopExpression is ScopedNode) { ((ScopedNode)LoopExpression).Parent = this; ChildNodes.Add(((ScopedNode)LoopExpression)); } } if (forBlock.ChildNodes[2].ChildNodes.Count > 0 && forBlock.ChildNodes[2].FirstChild.ChildNodes.Count > 0) { LoopStatement = StatementNode.GetStatement(forBlock.ChildNodes[2].FirstChild.FirstChild, context) as IStatement; if (LoopStatement != null) { ((ScopedNode)LoopStatement).Parent = this; ChildNodes.Add(((ScopedNode)LoopStatement)); } } ScopedNode LoopNode = StatementNode.GetStatement(treeNode.LastChild, context, false); Loop = LoopNode as IStatement; if (Loop != null) { ((ScopedNode)Loop).Parent = this; ChildNodes.Add(((ScopedNode)Loop)); } else { context.AddParserMessage(ParserErrorLevel.Error, treeNode.LastChild.Span, "Loop statement is invalid."); } if (!Options.HasOption(LanguageOption.UnBracedLoopsIfs) && !(Loop is BlockNode)) { context.AddParserMessage(ParserErrorLevel.Error, LoopNode.Span, "Statement must be enclosed in a block."); } if (LoopExpression == null || !(LoopInitializer is AssignmentNode || LoopInitializer is VariableDeclarationNode)) { if (Options != LanguageOption.Extended) // Extended will be normalized without error { if (!(LoopInitializer is AssignmentNode || LoopInitializer is VariableDeclarationNode || LoopInitializer == null)) { context.AddParserMessage(ParserErrorLevel.Error, forBlock.ChildNodes[0].Span, "Loop initializer must be empty, or an assignment or declaration."); } if (LoopExpression == null) { context.AddParserMessage(ParserErrorLevel.Error, forBlock.ChildNodes[1].Span, "Loop expression cannot be empty."); } } else { context.AddParserMessage(ParserErrorLevel.Info, forBlock.ChildNodes[0].Span, "Invalid For loop will be converted to a While."); } } }
public override void Init(ParsingContext context, ParseTreeNode parseNode) { base.Init(context, parseNode); List <Method> ScopeFuncs = UOSLBase.GetFuncs(context); CoreCommands.LoadCoreCommands(context); LanguageOption Options = context.GetOptions(); bool canLoadInherit = true; foreach (ParseTreeNode cnode in parseNode.ChildNodes) { ScopedNode toAdd = null; if (cnode.AstNode is InheritsNode) { if (canLoadInherit) { toAdd = (ScopedNode)cnode.AstNode; InheritsNode iNode = (InheritsNode)cnode.AstNode; if (Depends.ContainsKey(iNode.Filename)) { context.AddParserMessage(ParserErrorLevel.Error, cnode.Span, "File {0} is already loaded in this inheritance chain, cannot reload.", iNode.Filename); } else { m_grammar.LoadFile(iNode.Filename, this, context, Depends); } } else { context.AddParserMessage(ParserErrorLevel.Error, cnode.Span, "Inherits declaration(s) must appear first in the file, may not be made after other declarations."); } } else { canLoadInherit = false; if (cnode.AstNode is EventDefNode) { toAdd = (EventDefNode)cnode.AstNode; } else if (cnode.AstNode is UoCoreFunctionNode) { CoreCommands.Funcs.Add(new Method((FunctionProtoNode)cnode.AstNode, context)); } else if (cnode.AstNode is FunctionDefNode) { toAdd = (FunctionDefNode)cnode.AstNode; Method found; string name = ((FunctionDefNode)toAdd).NameString; if (ScopeFuncs != null && (found = ScopeFuncs.FirstOrDefault(func => func.Name == name)) != null && found.DefNode == null) { found.Define((FunctionDefNode)toAdd, context); } else { UOSLBase.AddFunc(new Method((FunctionDefNode)toAdd, context), context); } } else if (cnode.AstNode is FunctionProtoNode) { toAdd = (FunctionProtoNode)cnode.AstNode; UOSLBase.AddFunc(new Method((FunctionProtoNode)toAdd, context), context); } else if (cnode.AstNode is ConstantDeclarationNode) { ConstantDeclarationNode constnode = (ConstantDeclarationNode)cnode.AstNode; toAdd = constnode; } else if (cnode.AstNode is MemberDeclarationNode) { toAdd = (ScopedNode)cnode.AstNode; if (((MemberDeclarationNode)cnode.AstNode).Assign != null) { context.AddParserMessage(ParserErrorLevel.Warning, ((MemberDeclarationNode)cnode.AstNode).Assign.Span, "Member assignments in Declarations are not respected.", ((MemberDeclarationNode)cnode.AstNode).Declaration.Term.Name); } UOSLBase.AddMember(new Field((MemberDeclarationNode)cnode.AstNode, context), context); // global scope? } else if (cnode.AstNode is DeclarationNode) { toAdd = (DeclarationNode)cnode.AstNode; AddVar(new Field((DeclarationNode)toAdd, context), context); } } if (toAdd != null) { ChildNodes.Add(toAdd); toAdd.Parent = this; } } }