public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Check children LValueNode.CheckSemantics(scope, report); ExpressionNode.CheckSemantics(scope, report); if (!LValueNode.IsOK || !ExpressionNode.IsOK) { return; } //Check children types if (!LValueNode.TigerType.Assignable(ExpressionNode.TigerType)) { report.AddError(SemanticErrors.InvalidAssignType(ExpressionNode, ExpressionNode.TigerType, LValueNode.TigerType)); } var variableAccess = LValueNode as VariableAccessNode; if (variableAccess != null && !variableAccess.VariableInfo.Assignable) { report.AddError(SemanticErrors.NonAssignableVariable(LValueNode, variableAccess.VariableInfo.Name)); } IsOK = true; }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Check children LValueNode.CheckSemantics(scope, report); IndexExpressionNode.CheckSemantics(scope, report); if (!LValueNode.IsOK || !IndexExpressionNode.IsOK) { return; } //Check children types if (!(LValueNode.TigerType is ArrayType)) { report.AddError(SemanticErrors.NonArrayType(LValueNode, LValueNode.TigerType)); return; } if (!TigerType.Int.Assignable(IndexExpressionNode.TigerType)) { report.AddError(SemanticErrors.ArrayIndexerInvalidType(IndexExpressionNode, IndexExpressionNode.TigerType)); return; } TigerType = ((ArrayType)LValueNode.TigerType).ContentType; }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Check children LValueNode.CheckSemantics(scope, report); AttributeNode.CheckSemantics(scope, report); if (!LValueNode.IsOK || !AttributeNode.IsOK) { return; } //Check children types if (!(LValueNode.TigerType is RecordType)) { report.AddError(SemanticErrors.NonRecordType(LValueNode, LValueNode.TigerType)); } else { var field = ((RecordType)LValueNode.TigerType).RecordFields.FirstOrDefault(f => f.Name == AttributeNode.Name); if (field == null) { report.AddError(SemanticErrors.InvalidField(AttributeNode, AttributeNode.Name, LValueNode.TigerType.Name)); } else { TigerType = field.TigerType; } } }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Checking children Condition.CheckSemantics(scope, report); IfBlock.CheckSemantics(scope, report); ElseBlock.CheckSemantics(scope, report); if (!Condition.IsOK || !IfBlock.IsOK || !ElseBlock.IsOK) { return; } TigerType = IfBlock.TigerType; //Checking children types if (!TigerType.AreCompatible(Condition.TigerType, TigerType.Int)) { report.AddError(SemanticErrors.InvalidConditionType(Condition, Condition.TigerType)); } if (!TigerType.AreCompatible(IfBlock.TigerType, ElseBlock.TigerType)) { report.AddError(SemanticErrors.IncompatibleIfElseReturnType(ElseBlock, IfBlock.TigerType, ElseBlock.TigerType)); } }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Check children Condition.CheckSemantics(scope, report); IfBlock.CheckSemantics(scope, report); if (!Condition.IsOK && !IfBlock.IsOK) { return; } TigerType = TigerType.Void; //Check condition type if (!TigerType.AreCompatible(TigerType.Int, Condition.TigerType)) { report.AddError(SemanticErrors.InvalidConditionType(Condition, Condition.TigerType)); TigerType = TigerType.Error; } if (!TigerType.AreCompatible(TigerType.Void, IfBlock.TigerType)) { report.AddError(SemanticErrors.InvalidIfBodyType(IfBlock)); TigerType = TigerType.Error; } }
public void CheckBodySemantics(TigerScope scope, Report report) { //If CheckSemantics failed (FunctionInfo was not created) return if (!IsOK) { return; } IsOK = false; //Create function scope FunctionBodyNode.CheckSemantics(FunctionScope, report); if (!FunctionBodyNode.IsOK) { return; } IsOK = true; if (ReturnTypeNode != null && !ReturnTypeNode.TigerType.Assignable(FunctionBodyNode.TigerType)) { report.AddError(SemanticErrors.IncompatibleFunctionReturnTypeBody(FunctionBodyNode, ReturnTypeNode.TigerType, FunctionBodyNode.TigerType)); } else if (ReturnTypeNode == null && !TigerType.AreOfSameType(TigerType.Void, FunctionBodyNode.TigerType)) { report.AddError(SemanticErrors.IncompatibleFunctionReturnTypeBody(FunctionBodyNode, TigerType.Void, FunctionBodyNode.TigerType)); } }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Check children LeftOperandNode.CheckSemantics(scope, report); RightOperandNode.CheckSemantics(scope, report); if (!LeftOperandNode.IsOK || !RightOperandNode.IsOK) { return; } //Check children types if (TigerType.AreCompatible(LeftOperandNode.TigerType, TigerType.Int) && TigerType.AreCompatible(RightOperandNode.TigerType, TigerType.Int)) { TigerType = TigerType.Int; } else if (!TigerType.AreCompatible(LeftOperandNode.TigerType, TigerType.Int)) { report.AddError(SemanticErrors.LogicalOperandInvalidType(LeftOperandNode, LeftOperandNode.TigerType)); } else { report.AddError(SemanticErrors.LogicalOperandInvalidType(RightOperandNode, RightOperandNode.TigerType)); } }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Check children IdNode.CheckSemantics(scope, report); TypeNode.CheckSemantics(scope, report); ExpressionNode.CheckSemantics(scope, report); if (!IdNode.IsOK || !TypeNode.IsOK || !ExpressionNode.IsOK) { return; } //Check use of variable name if (!scope.VariableNameAvailable(IdNode.Name)) { report.AddError(SemanticErrors.VariableNameAlreadyInUse(IdNode, IdNode.Name)); return; } //Check children types if (!TypeNode.TigerType.Assignable(ExpressionNode.TigerType)) { report.AddError(SemanticErrors.InvalidAssignType(ExpressionNode, TypeNode.TigerType, ExpressionNode.TigerType)); } //Add variable to scope VariableInfo = scope.DefineVariable(IdNode.Name, TypeNode.TigerType, ContainingScope); IsOK = true; }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Check children Condition.CheckSemantics(scope, report); InstructionNode.CheckSemantics(scope, report); if (!Condition.IsOK || !InstructionNode.IsOK) { return; } IsOK = true; //Check children types if (!TigerType.AreCompatible(Condition.TigerType, TigerType.Int)) { report.AddError(SemanticErrors.InvalidConditionType(Condition, Condition.TigerType)); IsOK = false; } if (!TigerType.AreCompatible(InstructionNode.TigerType, TigerType.Void)) { report.AddError(SemanticErrors.InvalidWhileBodyType(InstructionNode)); IsOK = false; } }
public void SolveTypes() { for (int i = 0; i < TypeDeclarationNodes.Length; i++) { if (Sets[i] == null) { continue; } var parentSet = DisjointSet.SetOf(Sets[i]); if (parentSet.IsInvalid) { Scope.RemoveType(Sets[i].Name); Report.AddError(SemanticErrors.CircularTypeDefinition(TypeDeclarationNodes[i], TypeDeclarationNodes[i].IdNode.Name)); } else { DisjointSet.FirstArrayDependency(Sets[i]); if (Sets[i].TigerType is ArrayType) { ((ArrayType)Sets[i].TigerType).ContentType = Sets[i].Parent.TigerType; } if (Sets[i].TigerType is SimpleType) { ((SimpleType)Sets[i].TigerType).ActualType = Sets[i].Parent.TigerType; } if (!(TypeDeclarationNodes[i] is RecordTypeDeclarationNode)) { Scope.CompleteType(Sets[i].Name, Sets[i].TigerType); } } } }
public override void CheckBodySemantics(TigerScope scope, Report report) { //Check children IdNode.CheckSemantics(scope, report); FieldDeclarationNodes.ToList().ForEach(f => f.CheckSemantics(scope, report)); var usedNames = new List <string>(); foreach (var f in FieldDeclarationNodes) { if (usedNames.Contains(f.IdNode.Name)) { report.AddError(SemanticErrors.DuplicateFieldDeclaration(f, IdNode.Name, f.IdNode.Name)); } usedNames.Add(f.IdNode.Name); } //If type was not added to scope (if CheckSemantics failed) return if (!IsOK || FieldDeclarationNodes.Any(f => !f.IsOK)) { IsOK = false; return; } RecordTigerType = scope.FindType(IdNode.Name) as RecordType; IsOK = true; }
private object VisitQuestion(Question question) { if (question.IsComputed) { DataType expressionType = question.Expression.GetType(_symbolTable); if (expressionType != DataType.Undefined) { if (question.DataType != expressionType) { Report.AddError(question.Position, "Cannot assign a value of type '{0}' to question '{1}' of type '{2}'.", StringEnum.GetStringValue(expressionType), question.Id.Name, StringEnum.GetStringValue(question.DataType)); } } question.Expression.Accept(this); } // Check when the question is redeclared, it is of the same type. if (_symbolTable.ContainsKey(question.Id.Name)) { if (question.DataType != _symbolTable[question.Id.Name]) { Report.AddError(question.Position, "Cannot redeclare a question with a different type."); } } _symbolTable.Add(question.Id.Name, question.DataType); return(null); }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Check children IdNode.CheckSemantics(scope, report); if (!IdNode.IsOK) { return; } //Check type name existence if (!scope.TypeNameAvailable(IdNode.Name)) { report.AddError(SemanticErrors.TypeNameAlreadyInUse(IdNode, IdNode.Name)); } else { RecordTigerType = new RecordType(IdNode.Name, ContainingScope.Name, null); scope.DefineIncompleteType(IdNode.Name, RecordTigerType); } IsOK = true; }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Create new scope var childScope = new TigerScope(scope, "Let"); //Check children DeclarationListNode.CheckSemantics(childScope, report); InstructionSequenceNode.CheckSemantics(childScope, report); if (!DeclarationListNode.IsOK || !InstructionSequenceNode.IsOK) { return; } TigerType = InstructionSequenceNode.TigerType; //Check return type if (!childScope.ValidReturnType(TigerType)) { report.AddError(SemanticErrors.InvalidReturnType(this, TigerType)); } }
public override object VisitCheckBox(CheckBox checkBox) { if (!checkBox.SupportsDataType(_currentDataType)) { Report.AddError(checkBox.Position, MessageFormat, "checkbox", StringEnum.GetStringValue(_currentDataType)); } return(null); }
public override object VisitCalendar(Calendar calendar) { if (!calendar.SupportsDataType(_currentDataType)) { Report.AddError(calendar.Position, MessageFormat, "calendar", StringEnum.GetStringValue(_currentDataType)); } return(null); }
public override object Visit(DateLiteral literal) { if (!literal.IsValid) { Report.AddError(literal.Position, InvalidLiteralMessage, literal.Value, StringEnum.GetStringValue(literal.GetType(null))); } return(null); }
public override object Visit(Identifier identifier) { if (!_declaredQuestions.Contains(identifier.Name)) { Report.AddError(identifier.Position, "Identifier '{0}' does not reference a defined question.", identifier.Name); } return(null); }
public override object VisitDropDown(DropDown dropDown) { if (!dropDown.SupportsDataType(_currentDataType)) { Report.AddError(dropDown.Position, MessageFormat, "dropdown", StringEnum.GetStringValue(_currentDataType)); } return(null); }
public override object VisitRadioButtons(RadioButtons radioButtons) { if (!radioButtons.SupportsDataType(_currentDataType)) { Report.AddError(radioButtons.Position, MessageFormat, "radiobuttons", StringEnum.GetStringValue(_currentDataType)); } return(null); }
public override object VisitSpinBox(SpinBox spinBox) { if (!spinBox.SupportsDataType(_currentDataType)) { Report.AddError(spinBox.Position, MessageFormat, "spinbox", StringEnum.GetStringValue(_currentDataType)); } return(null); }
public override object VisitTextBox(TextBox textBox) { if (!textBox.SupportsDataType(_currentDataType)) { Report.AddError(textBox.Position, MessageFormat, "textbox", StringEnum.GetStringValue(_currentDataType)); } return(null); }
public override object VisitFontSize(FontSize fontSize) { if (fontSize.Size <= 0) { Report.AddError(fontSize.Position, "Style attribute 'fontsize' must have a value greater than 0."); } return(null); }
internal override void Validate(StyleSheet styleSheet, ValidationReport report) { base.Validate(styleSheet, report); foreach (var unrefferedQuestion in _unrefferedQuestions) { Report.AddError(TextPosition.None, "The QL question '{0}' is not referred in the QLS.", unrefferedQuestion); } }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Check children IdNode.CheckSemantics(scope, report); FromExpression.CheckSemantics(scope, report); ToExpression.CheckSemantics(scope, report); if (!IdNode.IsOK || !FromExpression.IsOK || !ToExpression.IsOK) { return; } //Check loop bounds type if (!TigerType.AreCompatible(FromExpression.TigerType, TigerType.Int) || !TigerType.AreCompatible(ToExpression.TigerType, TigerType.Int)) { report.AddError(!TigerType.AreCompatible(FromExpression.TigerType, TigerType.Int) ? SemanticErrors.InvalidForBoundType(FromExpression, FromExpression.TigerType) : SemanticErrors.InvalidForBoundType(ToExpression, ToExpression.TigerType)); return; } IsOK = true; //Define new scope TigerScope childScope = new TigerScope(scope, "For"); VariableInfo = childScope.DefineVariable(IdNode.Name, TigerType.Int, ContainingScope, false); DoInstruction.CheckSemantics(childScope, report); if (!DoInstruction.IsOK) { return; } if (!TigerType.AreCompatible(DoInstruction.TigerType, TigerType.Void)) { report.AddError(SemanticErrors.InvalidForBodyType(DoInstruction)); IsOK = false; } }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Check children TypeNode.CheckSemantics(scope, report); RecordFieldInitNodes.ToList().ForEach(f => f.CheckSemantics(scope, report)); if (!TypeNode.IsOK || RecordFieldInitNodes.Any(f => !f.IsOK)) { return; } //Check children types if (!(TypeNode.TigerType is RecordType)) { report.AddError(SemanticErrors.NonRecordType(TypeNode, TypeNode.TigerType)); } else { TigerType = TypeNode.TigerType; var fields = ((RecordType)TypeNode.TigerType).RecordFields; //Check fields length if (fields.Length != RecordFieldInitNodes.Length) { report.AddError(SemanticErrors.WrongNumberOfFields(TypeNode, TypeNode.TigerType.Name, fields.Length, RecordFieldInitNodes.Length)); } //Check field names and types for (int i = 0; i < Math.Min(fields.Length, RecordFieldInitNodes.Length); i++) { if (fields[i].Name != RecordFieldInitNodes[i].IdNode.Name) { report.AddError(SemanticErrors.WrongFieldPosition(RecordFieldInitNodes[i].IdNode, fields[i].Name, RecordFieldInitNodes[i].IdNode.Name)); } if (!fields[i].TigerType.Assignable(RecordFieldInitNodes[i].TigerType)) { report.AddError(SemanticErrors.InvalidFieldType(RecordFieldInitNodes[i].IdNode, fields[i].TigerType, RecordFieldInitNodes[i].TigerType)); } } } }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Checking children IdNode.CheckSemantics(scope, report); Arguments.ToList().ForEach(e => e.CheckSemantics(scope, report)); if (!IdNode.IsOK || Arguments.Any(e => !e.IsOK)) { return; } //Checking existence of function FunctionInfo = scope.FindFunction(IdNode.Name); if (FunctionInfo == null) { report.AddError(SemanticErrors.NonExistentFunctionReference(IdNode, IdNode.Name)); } else { TigerType = FunctionInfo.ReturnType; //Checking parameters if (Arguments.Length != FunctionInfo.ParameterNumber) { report.AddError(SemanticErrors.WrongNumberOfParameters(IdNode, FunctionInfo.Name, FunctionInfo.ParameterNumber, Arguments.Length)); } else { for (int i = 0; i < Arguments.Length; i++) { if (!FunctionInfo.Parameters[i].Assignable(Arguments[i].TigerType)) { report.AddError(SemanticErrors.InvalidArgumentType(Arguments[i], Arguments[i].TigerType, FunctionInfo.Parameters[i])); } } } } }
private object VisitQuestion(Question question) { if (!_declaredQuestions.Contains(question.Id.Name)) { _declaredQuestions.Add(question.Id.Name); } else { Report.AddError(question.Position, "A question with the name '{0}' is already defined in the same scope.", question.Id.Name); } return(null); }
private void ValidateUnaryExpression(UnaryExpression expression) { DataType operandType = expression.Operand.GetType(_symbolTable); if (operandType != DataType.Undefined) { if (!expression.OperandTypeIsValid(operandType)) { Report.AddError(expression.Position, "Operator '{0}' can not be applied to operand of type '{1}'.", StringEnum.GetStringValue(expression.Operation), operandType); } } }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Checking Children TypeNode.CheckSemantics(scope, report); LengthExpressionNode.CheckSemantics(scope, report); InitialValueExpressionNode.CheckSemantics(scope, report); if (!TypeNode.IsOK || !LengthExpressionNode.IsOK || !InitialValueExpressionNode.IsOK) { return; } var type = TypeNode.TigerType as ArrayType; if (type == null) { report.AddError(SemanticErrors.NonArrayType(TypeNode, TypeNode.TigerType)); return; } TigerType = TypeNode.TigerType; //Checking LengthExpression type if (!TigerType.AreCompatible(LengthExpressionNode.TigerType, TigerType.Int)) { report.AddError(SemanticErrors.NonIntegerArrayLength(LengthExpressionNode, LengthExpressionNode.TigerType)); } //Checking InitialValueExpression if (!type.ContentType.Assignable(InitialValueExpressionNode.TigerType)) { report.AddError(SemanticErrors.ArrayInitialValueInvalidType(InitialValueExpressionNode, InitialValueExpressionNode.TigerType, TypeNode.TigerType)); } }
private static Result<FileNode> Recognize(string path, string text, bool buildTree) { Report report = default(Report); CommandMarker commandMarker = new CommandMarker(); bool stopOnFirstError = true; bool newCommand = default(bool); ITree tree = default(ITree); text = MRecognizer.FixText(text); do { try { report = new Report(); newCommand = false; ANTLRStringStream characters = new ANTLRStringStream(text); MatlabLexer lexer = new MatlabLexer(characters, new Configuration(path, report, commandMarker, stopOnFirstError)); ExtendedTokenStream tokens = new ExtendedTokenStream(lexer, (int)Channel.Default); MatlabParser parser = new MatlabParser(tokens, new Configuration(path, report, commandMarker, stopOnFirstError)); var scope = parser.file(); tree = (ITree)scope.Tree; } catch (CommandException) { newCommand = true; } catch (StopException) { break; } catch (Exception ex) { report.AddError(path, 0, 0, ex.Message ?? string.Empty); } } while (report.IsOk && newCommand); if (report.IsOk && buildTree) { FileNode buildNode = TreeToNodeBuilder.Build(path, tree); return new Result<FileNode>(buildNode, new ReadOnlyReport(report)); } else { return new Result<FileNode>(null, new ReadOnlyReport(report)); } }