/// <summary> /// Visits a tree node /// </summary> /// <param name="interpreterTreeNode"></param> protected virtual void visitInterpreterTreeNode(InterpreterTreeNode interpreterTreeNode) { Designator designator = interpreterTreeNode as Designator; if (designator != null) { VisitDesignator(designator); } Term term = interpreterTreeNode as Term; if (term != null) { VisitTerm(term); } Expression expression = interpreterTreeNode as Expression; if (expression != null) { VisitExpression(expression); } Statement.Statement statement = interpreterTreeNode as Statement.Statement; if (statement != null) { VisitStatement(statement); } }
/// <summary> /// Gets the reference of the designator if it covers the position given /// </summary> /// <param name="designator"></param> protected override void VisitDesignator(Designator designator) { if (ShouldCheck(designator)) { Type type = designator.Ref as Type; if (Context == null && type != null) { Context = type; } ITypedElement element = designator.Ref as ITypedElement; if (Context == null && element != null) { Context = element; } ICallable callable = designator.Ref as ICallable; if (Context == null && callable != null) { Context = callable; } NameSpace nameSpace = designator.Ref as NameSpace; if (Context == null && nameSpace != null) { Context = nameSpace; } } }
/// <summary> /// Adds an expression as a parameter /// </summary> /// <param name="designator">the name of the actual parameter</param> /// <param name="expression">the actual parameter value</param> public void AddActualParameter(Designator designator, Expression expression) { if (designator == null) { ActualParameters.Add(expression); } else { bool found = false; foreach (KeyValuePair <Designator, Expression> pair in NamedActualParameters) { if (pair.Key.Image == designator.Image) { found = true; break; } } if (!found) { NamedActualParameters[designator] = expression; } else { AddError("Actual parameter " + designator.Image + " is bound twice", RuleChecksEnum.ExecutionFailed); } } expression.Enclosing = this; }
/// <summary> /// Checks the expression and appends errors to the root tree node when inconsistencies are found /// </summary> public override void CheckExpression() { Structure structureType = Structure.GetExpressionType() as Structure; if (structureType != null) { if (structureType.IsAbstract) { AddError("Instantiation of abstract types is forbidden", RuleChecksEnum.SyntaxError); } foreach (KeyValuePair <Designator, Expression> pair in Associations) { Designator name = pair.Key; Expression expression = pair.Value; List <INamable> targets = new List <INamable>(); structureType.Find(name.Image, targets); if (targets.Count > 0) { expression.CheckExpression(); Type type = expression.GetExpressionType(); if (type != null) { if (type.IsAbstract) { AddError("Instantiation of abstract types is forbidden", RuleChecksEnum.SyntaxError); } else { foreach (INamable namable in targets) { ITypedElement element = namable as ITypedElement; if (element != null && element.Type != null) { if (!element.Type.Match(type)) { AddError("Expression " + expression + " type (" + type.FullName + ") does not match the target element " + element.Name + " type (" + element.Type.FullName + ")", RuleChecksEnum.SyntaxError); } } } } } else { AddError("Expression " + expression + " type cannot be found", RuleChecksEnum.SyntaxError); } } else { Root.AddError("Cannot find " + name + " in structure " + Structure); } } } else { AddError("Cannot find structure type " + Structure, RuleChecksEnum.SyntaxError); } }
/// <summary> /// Fills the list provided with the element matching the filter provided /// </summary> /// <param name="retVal">The list to be filled with the element matching the condition expressed in the filter</param> /// <param name="filter">The filter to apply</param> public void Fill(List <INamable> retVal, BaseFilter filter) { if (Designator != null) { Designator.Fill(retVal, filter); } else if (LiteralValue != null) { LiteralValue.Fill(retVal, filter); } }
/// <summary> /// Provides the element called by this term, if any /// </summary> /// <param name="context">The context on which the variable must be found</param> /// <param name="explain"></param> /// <returns></returns> public ICallable GetCalled(InterpretationContext context, ExplanationPart explain) { ICallable retVal = null; if (Designator != null) { retVal = Designator.GetCalled(context); } return(retVal); }
/// <summary> /// Provides the variable referenced by this expression, if any /// </summary> /// <param name="context">The context on which the variable must be found</param> /// <returns></returns> public IVariable GetVariable(InterpretationContext context) { IVariable retVal = null; if (Designator != null) { retVal = Designator.GetVariable(context); } return(retVal); }
/// <summary> /// Performs the semantic analysis of the term /// </summary> /// <param name="instance">the reference instance on which this element should analysed</param> /// <param name="expectation">Indicates the kind of element we are looking for</paraparam> /// <param name="lastElement">Indicates that this element is the last one in a dereference chain</param> /// <returns>True if semantic analysis should be continued</returns> public void SemanticAnalysis(Utils.INamable instance, Filter.AcceptableChoice expectation, bool lastElement) { if (Designator != null) { Designator.SemanticAnalysis(instance, expectation, lastElement); } else if (LiteralValue != null) { LiteralValue.SemanticAnalysis(instance, expectation); } }
/// <summary> /// Fills the list provided with the element matching the filter provided /// </summary> /// <param name="retVal">The list to be filled with the element matching the condition expressed in the filter</param> /// <param name="filter">The filter to apply</param> public void fill(List <Utils.INamable> retVal, Filter.AcceptableChoice filter) { if (Designator != null) { Designator.fill(retVal, filter); } else if (LiteralValue != null) { LiteralValue.fill(retVal, filter); } }
/// <summary> /// Provides the element called by this term, if any /// </summary> /// <param name="context">The context on which the variable must be found</param> /// <returns></returns> public ICallable getCalled(InterpretationContext context) { ICallable retVal = null; if (Designator != null) { retVal = Designator.getCalled(context); } return(retVal); }
/// <summary> /// Checks the expression and appends errors to the root tree node when inconsistencies are found /// </summary> public void checkExpression() { if (Designator != null) { Designator.checkExpression(); } else if (LiteralValue != null) { LiteralValue.checkExpression(); } }
/// <summary> /// Performs the semantic analysis of the term /// </summary> /// <param name="instance">the reference instance on which this element should analysed</param> /// <param name="expectation">Indicates the kind of element we are looking for</param> /// <param name="lastElement">Indicates that this element is the last one in a dereference chain</param> /// <returns>True if semantic analysis should be continued</returns> public void SemanticAnalysis(INamable instance, BaseFilter expectation, bool lastElement) { if (Designator != null) { Designator.SemanticAnalysis(instance, expectation, lastElement); StaticUsage = Designator.StaticUsage; } else if (LiteralValue != null) { LiteralValue.SemanticAnalysis(instance, expectation); StaticUsage = LiteralValue.StaticUsage; } }
/// <summary> /// Provides the designator at position Index of the Buffer. /// </summary> /// <param name="root">The root element for which this designator is built</param> /// <returns>null if the element at position Index is not an identifier</returns> private Designator Designator() { Designator retVal = null; skipWhiteSpaces(); string identifier = Identifier(); if (identifier != null) { retVal = new Designator(Root, identifier); } return(retVal); }
/// <summary> /// Indicates whether this expression references an instance /// </summary> /// <returns></returns> public bool IsInstance() { bool retVal = false; if (LiteralValue != null) { retVal = true; } else if (Designator != null) { retVal = Designator.IsInstance(); } return(retVal); }
/// <summary> /// Provides the value associated to this Expression /// </summary> /// <param name="context">The context on which the value must be found</param> /// <param name="explain"></param> /// <returns></returns> public IValue GetValue(InterpretationContext context, ExplanationPart explain) { IValue retVal = null; if (Designator != null) { retVal = Designator.GetValue(context); } else if (LiteralValue != null) { retVal = LiteralValue.GetValue(context, explain); } return(retVal); }
/// <summary> /// Provides the value associated to this Expression /// </summary> /// <param name="context">The context on which the value must be found</param> /// <returns></returns> public Values.IValue GetValue(InterpretationContext context) { Values.IValue retVal = null; if (Designator != null) { retVal = Designator.GetValue(context); } else if (LiteralValue != null) { retVal = LiteralValue.GetValue(context); } return(retVal); }
public override string ToString() { string retVal = null; if (Designator != null) { retVal = Designator.ToString(); } else if (LiteralValue != null) { retVal = LiteralValue.ToString(); } return(retVal); }
/// <summary> /// Provides the possible references for this term (only available during semantic analysis) /// </summary> /// <param name="instance">the instance on which this element should be found.</param> /// <param name="expectation">the expectation on the element found</param> /// <param name="last">indicates that this is the last element in a dereference chain</param> /// <returns></returns> public ReturnValue getReferences(Utils.INamable instance, Filter.AcceptableChoice expectation, bool last) { ReturnValue retVal = null; if (Designator != null) { retVal = Designator.getReferences(instance, expectation, last); } else if (LiteralValue != null) { retVal = LiteralValue.getReferences(instance, expectation, last); } return(retVal); }
/// <summary> /// Provides the type of this expression /// </summary> /// <param name="context">The interpretation context</param> /// <returns></returns> public Types.Type GetExpressionType() { Types.Type retVal = null; if (Designator != null) { retVal = Designator.GetDesignatorType(); } else if (LiteralValue != null) { retVal = LiteralValue.GetExpressionType(); } return(retVal); }
/// <summary> /// Provides the possible references for this term (only available during semantic analysis) /// </summary> /// <param name="instance">the instance on which this element should be found.</param> /// <param name="expectation">the expectation on the element found</param> /// <param name="last">indicates that this is the last element in a dereference chain</param> /// <returns></returns> public ReturnValue GetReferences(INamable instance, BaseFilter expectation, bool last) { ReturnValue retVal = null; if (Designator != null) { retVal = Designator.GetReferences(instance, expectation, last); } else if (LiteralValue != null) { retVal = LiteralValue.GetReferences(instance, expectation, last); } return(retVal); }
protected override void VisitDesignator(Designator designator) { base.VisitDesignator(designator); Utils.ModelElement current = designator.Ref as Utils.ModelElement; while (current != null && !(current is NameSpace) && !(current is Parameter)) { bool change; Variable variable = current as Variable; if (variable != null) { change = variable.AddDependancy(DependantFunction); DependancyChange = DependancyChange || change; } else { StructureElement structureElement = current as StructureElement; if (structureElement != null) { change = structureElement.AddDependancy(DependantFunction); DependancyChange = DependancyChange || change; change = structureElement.Structure.AddDependancy(DependantFunction); DependancyChange = DependancyChange || change; } else { Function function = current as Function; if (function != null) { change = function.AddDependancy(DependantFunction); DependancyChange = DependancyChange || change; } } } current = current.Enclosing as Utils.ModelElement; } }
/// <summary> /// Provides the Term at position Index of the Buffer. /// </summary> /// <param name="root">The root element for which this term is built</param> /// <returns></returns> public Term Term() { Term retVal = null; Expression literalValue = EvaluateLiteral(); if (literalValue != null) { retVal = new Term(Root, literalValue); } if (retVal == null) { Designator designator = Designator(); if (designator != null) { retVal = new Term(Root, designator); } } return(retVal); }
/// <summary> /// Provides the possible references types for this expression (used in semantic analysis) /// </summary> /// <param name="instance">the reference instance on which this element should analysed</param> /// <paraparam name="expectation">Indicates the kind of element we are looking for</paraparam> /// <param name="last">indicates that this is the last element in a dereference chain</param> /// <returns></returns> public ReturnValue getReferenceTypes(Utils.INamable instance, Filter.AcceptableChoice expectation, bool last) { ReturnValue retVal = null; if (Designator != null) { retVal = new ReturnValue(); foreach (ReturnValueElement element in Designator.getReferences(instance, expectation, last).Values) { if (element.Value is Types.Type) { retVal.Add(element.Value); } } } else if (LiteralValue != null) { retVal = LiteralValue.getReferenceTypes(instance, expectation, true); } return(retVal); }
/// <summary> /// Visits an interpreter tree node /// </summary> /// <param name="interpreterTreeNode"></param> protected virtual void VisitInterpreterTreeNode(InterpreterTreeNode interpreterTreeNode) { Expression expression = interpreterTreeNode as Expression; if (expression != null) { VisitExpression(expression); } else { Statement.Statement statement = interpreterTreeNode as Statement.Statement; if (statement != null) { VisitStatement(statement); } else { Term term = interpreterTreeNode as Term; if (term != null) { VisitTerm(term); } else { Designator designator = interpreterTreeNode as Designator; if (designator != null) { VisitDesignator(designator); } else { throw new NotImplementedException(); } } } } }
/// <summary> /// Provides the possible references types for this expression (used in semantic analysis) /// </summary> /// <param name="instance">the reference instance on which this element should analysed</param> /// <param name="expectation">Indicates the kind of element we are looking for</param> /// <param name="last">indicates that this is the last element in a dereference chain</param> /// <returns></returns> public ReturnValue GetReferenceTypes(INamable instance, BaseFilter expectation, bool last) { ReturnValue retVal = null; if (Designator != null) { retVal = new ReturnValue(); foreach (ReturnValueElement element in Designator.GetReferences(instance, expectation, last).Values) { if (element.Value is Type) { const bool asType = true; retVal.Add(element.Value, null, asType); } } } else if (LiteralValue != null) { retVal = LiteralValue.GetReferenceTypes(instance, expectation, true); } return(retVal); }
/// <summary> /// Constructor /// </summary> /// <param name="root">The root element for which this model is built</param> /// <param name="designator"></parparam> public Term(ModelElement root, Designator designator) : base(root) { Designator = designator; Designator.Enclosing = this; }
/// <summary> /// Provides the designator at position Index of the Buffer. /// </summary> /// <param name="root">The root element for which this designator is built</param> /// <returns>null if the element at position Index is not an identifier</returns> private Designator Designator() { Designator retVal = null; skipWhiteSpaces(); string identifier = Identifier(); if (identifier != null) { retVal = new Designator(Root, identifier); } return retVal; }
/// <summary> /// Gets the reference of the designator if it covers the position given /// </summary> /// <param name="designator"></param> protected override void VisitDesignator(Designator designator) { if (ShouldCheck(designator)) { Type type = designator.Ref as Type; if ( Context == null && type != null ) { Context = type; } ITypedElement element = designator.Ref as ITypedElement; if (Context == null && element != null) { Context = element; } ICallable callable = designator.Ref as ICallable; if (Context == null && callable != null) { Context = callable; } NameSpace nameSpace = designator.Ref as NameSpace; if (Context == null && nameSpace != null) { Context = nameSpace; } } }
/// <summary> /// Evaluates the current input as a structure /// </summary> /// <returns></returns> public Expression EvaluateStructure() { StructExpression retVal = null; SkipWhiteSpaces(); int start = Index; Expression structureId = DerefExpression(); if (structureId != null) { if (LookAhead("{")) { Match("{"); Dictionary<Designator, Expression> associations = new Dictionary<Designator, Expression>(); if (LookAhead("}")) { Match("}"); retVal = new StructExpression(Root, RootLog, structureId, associations, start, Index); } else { while (true) { SkipWhiteSpaces(); int startId = Index; string id = Identifier(); if (id != null) { Designator designator = new Designator(Root, RootLog, id, startId, startId + id.Length); string assignOp = LookAhead(AssignOps); if (assignOp != null) { Match(assignOp); Expression expression = Expression(0); if (expression != null) { associations[designator] = expression; } else { RootLog.AddError("Cannot parse expression after " + id + " " + assignOp + " "); break; } } else { throw new ParseErrorException("<- or => expected", Index, Buffer); } } else { if (Index < Buffer.Length) { RootLog.AddError("Identifier expected, but found " + Buffer[Index]); } else { RootLog.AddError("Identifier expected, but EOF found "); } break; } if (LookAhead(",")) { Match(","); } else if (LookAhead("}")) { Match("}"); retVal = new StructExpression(Root, RootLog, structureId, associations, start, Index); break; } else { if (Index < Buffer.Length) { RootLog.AddError(", or } expected, but found " + Buffer[Index]); } else { RootLog.AddError(", or } expected, but EOF found "); } break; } } } } } if (retVal == null) { Index = start; } return retVal; }
/// <summary> /// Constructor /// </summary> /// <param name="root">The root element for which this model is built</param> /// <param name="log"></param> /// <param name="designator"></param> /// <param name="start">The start character for this expression in the original string</param> /// <param name="end">The end character for this expression in the original string</param> public Term(ModelElement root, ModelElement log, Designator designator, int start, int end) : base(root, log, start, end) { Designator = SetEnclosed(designator); }
/// <summary> /// Provides the designator at position Index of the Buffer. /// </summary> /// <returns>null if the element at position Index is not an identifier</returns> private Designator Designator() { Designator retVal = null; SkipWhiteSpaces(); int start = Index; string identifier = Identifier(); if (identifier != null) { retVal = new Designator(Root, RootLog, identifier, start, start + identifier.Length); } return retVal; }
/// <summary> /// Evaluates a function call, when the left part (function identification) has been parsed /// </summary> /// <param name="left">The left part of the function call expression</param> /// <returns></returns> private Expression EvaluateFunctionCallExpression(Expression left) { Call retVal = null; SkipWhiteSpaces(); if (LookAhead("(")) { retVal = new Call(Root, RootLog, left, left.Start, -1); Match("("); bool cont = true; while (cont) { SkipWhiteSpaces(); if (LookAhead(")")) { Match(")"); cont = false; } else { // Handle named parameters int current2 = Index; string id = Identifier(); Designator parameter = null; if (id != null) { string assignOp = LookAhead(AssignOps); if (assignOp != null) { Match(assignOp); parameter = new Designator(Root, RootLog, id, current2, current2 + id.Length); } else { Index = current2; } } Expression arg = Expression(0); if (arg != null) { retVal.AddActualParameter(parameter, arg); if (LookAhead(",")) { Match(","); } else if (LookAhead(")")) { Match(")"); cont = false; } } else { throw new ParseErrorException("Syntax error", Index, Buffer); } } } retVal.End = Index; } return retVal; }
/// <summary> /// Visits a designator /// </summary> /// <param name="designator"></param> protected virtual void VisitDesignator(Designator designator) { }
/// <summary> /// Adds an expression as a parameter /// </summary> /// <param name="designator">the name of the actual parameter</param> /// <param name="expression">the actual parameter value</param> public void AddActualParameter(Designator designator, Expression expression) { if (designator == null) { ActualParameters.Add(expression); } else { bool found = false; foreach (KeyValuePair<Designator, Expression> pair in NamedActualParameters) { if (pair.Key.Image == designator.Image) { found = true; break; } } if (!found) { NamedActualParameters[designator] = expression; } else { AddError("Actual parameter " + designator.Image + " is bound twice"); } } expression.Enclosing = this; }
/// <summary> /// Constructor /// </summary> /// <param name="root">The root element for which this model is built</param> /// <param name="log"></param> /// <param name="designator"></param> /// <param name="parsingData">Additional information about the parsing process</param> public Term(ModelElement root, ModelElement log, Designator designator, ParsingData parsingData) : base(root, log, parsingData) { Designator = SetEnclosed(designator); }
/// <summary> /// Creates a redef expression based on the input of the parser /// </summary> /// <returns></returns> private Expression DerefExpression() { Expression retVal = null; List<Expression> derefArguments = new List<Expression>(); SkipWhiteSpaces(); int start = Index; string id = Identifier(); while (id != null) { Designator designator = new Designator(Root, RootLog, id, start, start + id.Length); Term term = new Term(Root, RootLog, designator, designator.Start, designator.End); UnaryExpression unaryExpression = new UnaryExpression(Root, RootLog, term, term.Start, term.End); derefArguments.Add(unaryExpression); id = null; if (LookAhead(".")) { Match("."); SkipWhiteSpaces(); start = Index; id = Identifier(); } } if (derefArguments.Count == 1) { retVal = derefArguments[0]; } else if (derefArguments.Count > 1) { retVal = new DerefExpression(Root, RootLog, derefArguments, derefArguments[0].Start, derefArguments[derefArguments.Count - 1].End); } return retVal; }
/// <summary> /// Constructor /// </summary> /// <param name="root">The root element for which this model is built</param> /// <param name="designator"></parparam> /// <param name="start">The start character for this expression in the original string</param> /// <param name="end">The end character for this expression in the original string</param> public Term(ModelElement root, ModelElement log, Designator designator, int start, int end) : base(root, log, start, end) { Designator = designator; Designator.Enclosing = this; }