SubtypeDeclaration ParseSubtypeDeclaration() { ReadNextToken(); //skip subtype string identifier = fCurrentToken.Value; ReadNextToken(); //skip identifier fCurrentToken = fLexicalAnalyser.SkipExpected(TokenType.Word, "is"); SubtypeIndication subtype = ParseSubtypeIndication(); //Read until end of line fCurrentToken = fLexicalAnalyser.SkipExpected(TokenType.Symbol, ";"); SubtypeDeclaration ParsedSubtype = new SubtypeDeclaration(identifier, subtype); SubtypeList.Add(ParsedSubtype); return(ParsedSubtype); }
// This method is for determining the specified attribute for a type. // Currently only 3 attributes have been defined (left, right and length). // Attributes can be called on types or subtypes, therefore cases for each are specified below. int DetermineAttribute(string Attribute, string Type) { // It is first determined whether the subtype passed exists in the list of subtypes. if (SubtypeList.Exists(x => x.Identifier == Type)) { // The subtype is found and stored in chosenType. SubtypeDeclaration chosenType = SubtypeList.Find(x => x.Identifier == Type); // The correct attribute is fetched depending on the passed string. if (Attribute == "left") { return(chosenType.Left); } if (Attribute == "right") { return(chosenType.Right); } if (Attribute == "high") { return((chosenType.Right >= chosenType.Left) ? chosenType.Right : chosenType.Left); } if (Attribute == "low") { return(chosenType.Right >= chosenType.Left ? chosenType.Left : chosenType.Right); } if (Attribute == "length") { return(Math.Abs(chosenType.Left - chosenType.Right) + 1); } throw new ParserException("Attribute Provided is not recognised: " + Attribute); } else if (EnumerationTypeList.Exists(x => x.Identifier == Type)) { EnumerationTypeDeclaration chosenType = EnumerationTypeList.Find(x => x.Identifier == Type); if (Attribute == "left") { return(chosenType.Left); } if (Attribute == "right") { return(chosenType.Right); } if (Attribute == "length") { return(Math.Abs(chosenType.Left - chosenType.Right) + 1); } throw new ParserException("Attribute Provided is not recognised"); } // If the type passed is neither a subtype or a type an exception is thrown. else { throw new ParserException("Cannot determine attribute, Type/Subtype not recognized."); } }