/// <summary> /// Parser for EachStatement /// </summary> /// <returns>Parsed EachStatement</returns> public EachStatement ParseEachStatement() { EachStatement eachStatement = new EachStatement(); //Skip each token NextToken("each", "each (identifier : expression) Statement", "each"); //Skip ( token NextToken("(", "each (identifier : expression) Statement", '('); //Parse identifier CurrentToken = TokenStream.NextToken(); eachStatement.SetIdentifier(CurrentToken.GetValue().ToString()); //Skip : token NextToken(":", "each (identifier : expression) Statement", ':'); //Parse expression eachStatement.SetExpression(expressionParser.ParseExpression()); //Skip ) token NextToken(")", "each (identifier : expression) Statement", ')'); //Parse statement eachStatement.SetStatement(ParseStatement()); return(eachStatement); }
/// <summary> /// Parse a module identifier /// </summary> /// <param name="identifier">Identifier to parse</param> public ModuleId ParseModuleId() { ModuleId moduleId = new ModuleId(); //parse module identifier while (TokenStream.HasNext()) { if (NextToken("identifier", "module identifier.identifier", TokenType.IDENTIFIER)) { moduleId.AddIdentifier(CurrentToken.GetValue().ToString()); } else { //Raise exception throw new UnexpectedToken("Unexpected token found:", CurrentToken.GetValue().ToString(), CurrentToken.GetLine()); } if (TokenStream.HasNext() && TokenStream.Peek(1).GetValue().ToString() == ".") { //Period, so another identifier will appear after this one NextToken(".", "module identifier.identifier", '.'); } else { break; //No more module identifier stuff will appear } } return(moduleId); }
/// <summary> /// Parser for Formal /// </summary> /// <returns>Parsed Formal</returns> public Formal ParseFormal() { Formal formal = new Formal(); CurrentToken = TokenStream.NextToken(); formal.SetIdentifier(CurrentToken.GetValue().ToString()); return(formal); }
/// <summary> /// Parse an TextExpression /// </summary> /// <returns>Parsed TextExpression</returns> public TextExpression ParseTextExpression() { TextExpression textExpression = new TextExpression(); CurrentToken = TokenStream.NextToken(); textExpression.SetText(CurrentToken.GetValue().ToString()); return(textExpression); }
/// <summary> /// Parse an NumericExpression /// </summary> /// <returns>Parsed Numeric Expression</returns> public NumExpression ParseNumExpression() { NumExpression numExpression = new NumExpression(); CurrentToken = TokenStream.NextToken(); numExpression.SetNum(ObjectToInt(CurrentToken.GetValue())); return(numExpression); }
/// <summary> /// Parse an SymbolExpression /// </summary> /// <returns>Parsed SymbolExpression</returns> public SymExpression ParseSymExpression() { SymExpression symExpression = new SymExpression(); CurrentToken = TokenStream.NextToken(); symExpression.SetSym(CurrentToken.GetValue().ToString()); return(symExpression); }
/// <summary> /// Parse an VariableExpression /// </summary> /// <returns>Parsed VariableExpression</returns> public VarExpression ParseVarExpression() { VarExpression varExpression = new VarExpression(); CurrentToken = TokenStream.NextToken(); varExpression.SetVariableIdentifier(CurrentToken.GetValue().ToString()); return(varExpression); }
/// <summary> /// Parser for WidthAttribute /// </summary> /// <returns>Parsed HeightAttribute</returns> public WidthAttribute ParseWidthAttribute() { WidthAttribute widthAttribute = new WidthAttribute(); //Get height CurrentToken = TokenStream.NextToken(); widthAttribute.SetWidth(ObjectToInt(CurrentToken.GetValue())); return(widthAttribute); }
/// <summary> /// Parser for TypeAttribute /// </summary> /// <returns>Parsed TypeAttribute</returns> public TypeAttribute ParseTypeAttribute() { TypeAttribute typeAttribute = new TypeAttribute(); //Get type token CurrentToken = TokenStream.NextToken(); typeAttribute.SetType(CurrentToken.GetValue().ToString()); return(typeAttribute); }
/// <summary> /// Parser for NameAttribute /// </summary> /// <returns>Parsed NameAttribute</returns> public NameAttribute ParseNameAttribute() { NameAttribute nameAttribute = new NameAttribute(); //Get name token CurrentToken = TokenStream.NextToken(); nameAttribute.SetName(CurrentToken.GetValue().ToString()); return(nameAttribute); }
/// <summary> /// Parser for ClassAttribute /// </summary> /// <returns>Parsed ClassAttribute</returns> public ClassAttribute ParseClassAttribute() { ClassAttribute classAttribute = new ClassAttribute(); //Get class token CurrentToken = TokenStream.NextToken(); classAttribute.SetClass(CurrentToken.GetValue().ToString()); return(classAttribute); }
/// <summary> /// Parser for IdAttribute /// </summary> /// <returns>Parsed IdAttribute</returns> public IdAttribute ParseIdAttribute() { IdAttribute idAttribute = new IdAttribute(); //Get id token CurrentToken = TokenStream.NextToken(); idAttribute.SetId(CurrentToken.GetValue().ToString()); return(idAttribute); }
/// <summary> /// Parser for TextChars /// </summary> /// <returns>Parsed TextChars as String</returns> public String ParseTextChars() { StringBuilder stringBuilder = new StringBuilder(); while (EmbeddingTokenStream.HasNext() && EmbeddingTokenStream.Peek(1).GetType() == TokenType.TEXT) { //Parse all text tokens until different type found CurrentToken = EmbeddingTokenStream.NextToken(); stringBuilder.Append(CurrentToken.GetValue().ToString()); } return(stringBuilder.ToString()); }
/// <summary> /// Get NextToken and verify if it exists (EmbeddedTokenStream) /// </summary> /// <param name="name">Name of expected token</param> /// <param name="syntax">Syntax of expected token</param> /// <returns>True if new token found, otherwise false</returns> private new bool NextToken(String name, String syntax) { if (EmbeddingTokenStream.HasNext()) { CurrentToken = EmbeddingTokenStream.NextToken(); return(true); } else { throw new UnexpectedToken("Unexpected token found:", CurrentToken.GetValue().ToString(), CurrentToken.GetLine()); } }
/// <summary> /// Get NextToken and verify it maches matchObject (EmbeddedTokenStream) /// </summary> /// <param name="name">Name of expected token</param> /// <param name="syntax">Type of expected token</param> /// <param name="matchObject">Type of expected token</param> /// <returns>True if token matches, otherwise false</returns> private new bool NextToken(String name, String syntax, Object matchObject) { if (NextToken(name, syntax)) { if (CurrentToken.GetValue().Equals(matchObject)) { return(true); } else { throw new UnexpectedToken("Unexpected token found:", CurrentToken.GetValue().ToString(), CurrentToken.GetLine()); } } return(false); }
/// <summary> /// Parse import /// </summary> /// <returns></returns> public Import ParseImport() { Import import = new Import(); if (TokenStream.HasNext() && TokenStream.Peek(1).GetType() == TokenType.IDENTIFIER) { import.SetModuleId(ParseModuleId()); } else { //Raise exception throw new UnexpectedToken("Unexpected token found:", CurrentToken.GetValue().ToString(), CurrentToken.GetLine()); } return(import); }
/// <summary> /// Get NextToken and verify type of token (EmbeddedTokenStream) /// </summary> /// <param name="name">Name of expected token</param> /// <param name="syntax">Syntax of expected token</param> /// <param name="type">Type of expected token</param> /// <returns>True if token found and type matches, otherwise false</returns> private new bool NextToken(String name, String syntax, TokenType type) { if (NextToken(name, syntax)) { if (type.Equals(CurrentToken.GetType())) { return(true); } else { throw new UnexpectedToken("Unexpected token found:", CurrentToken.GetValue().ToString(), CurrentToken.GetLine()); } } return(false); }
/// <summary> /// Parse an expression /// </summary> /// <returns>Parsed Expression</returns> public Expression ParseExpression() { Expression expression = null; //Determine subtype and then parse that type if (TokenStream.Peek(1).GetType() == TokenType.TEXT) { //Text expression expression = ParseTextExpression(); } else if (TokenStream.Peek(1).GetType() == TokenType.WAEBRICSYMBOL) { //Symbol expression expression = ParseSymExpression(); } else if (TokenStream.Peek(1).GetType() == TokenType.NUMBER) { //Numeric expression expression = ParseNumExpression(); } else if (TokenStream.Peek(1).GetType() == TokenType.IDENTIFIER) { //Variable expression expression = ParseVarExpression(); } else if (TokenStream.Peek(1).GetValue().ToString() == "[") { //List expression expression = ParseListExpression(); } else if (TokenStream.Peek(1).GetValue().ToString() == "{") { //Record expression expression = ParseRecordExpression(); } //Check if it is maybe an catenation or field if (TokenStream.HasNext(2) && TokenStream.Peek(1).GetValue().ToString() == "." && TokenStream.Peek(2).GetType() == TokenType.IDENTIFIER && TokenStream.Peek(3).GetValue().ToString() != "?") { //Field return(ParseFieldExpression(expression)); } else if (TokenStream.HasNext() && TokenStream.Peek(1).GetValue().ToString() == "+") { //Catenation return(ParseCatExpression(expression)); } if (expression == null) { //No expression found, raise exception throw new UnexpectedToken("Expression expected, but found:", CurrentToken.GetValue().ToString(), CurrentToken.GetLine()); } return(expression); }
/// <summary> /// Parser for AttrArgument /// </summary> /// <returns>Parsed AttrArgument</returns> public AttrArgument ParseAttrArgument() { AttrArgument argument = new AttrArgument(); //Parse identifier CurrentToken = TokenStream.NextToken(); argument.SetIdentifier(CurrentToken.GetValue().ToString()); //Skip = token NextToken("=", "identifier = expression", '='); //Parse expression argument.SetExpression(expressionParser.ParseExpression()); return(argument); }
/// <summary> /// Parser for Assignment /// </summary> /// <returns>Parsed Assignment</returns> public Assignment ParseAssignment() { //Determine type if (TokenStream.HasNext(2) && TokenStream.Peek(2).GetValue().ToString() == "(") { //FuncBindAssignment return(ParseFuncBindAssignment()); } else if (TokenStream.HasNext(2) && TokenStream.Peek(2).GetValue().ToString() == "=") { //VarBindAssignment return(ParseVarBindAssignment()); } else { throw new UnexpectedToken("Assignment expected, found:", CurrentToken.GetValue().ToString(), CurrentToken.GetLine()); } }
/// <summary> /// Parser for CommentStatement /// </summary> /// <returns>Parsed CommentStatement</returns> public CommentStatement ParseCommentStatement() { CommentStatement commentStatement = new CommentStatement(); //Skip comment token NextToken("comment", "comment thisisacomment;", "comment"); //Parse comment CurrentToken = TokenStream.NextToken(); commentStatement.SetCommentString(CurrentToken.GetValue().ToString()); //Skip ; token NextToken(";", "comment thisisacomment;", ';'); return(commentStatement); }
/// <summary> /// Parse an KeyValuePair /// </summary> /// <returns>Parsed KeyValuePair</returns> public KeyValuePair ParseKeyValuePair() { KeyValuePair keyValuePair = new KeyValuePair(); //Get key CurrentToken = TokenStream.NextToken(); keyValuePair.SetKey(CurrentToken.GetValue().ToString()); //Skip : NextToken(":", "key : value", ':'); //Get value keyValuePair.SetValue(ParseExpression()); return(keyValuePair); }
/// <summary> /// Parse an FieldExpression /// </summary> /// <param name="expression">Expression which is already parsed</param> /// <returns></returns> public Expression ParseFieldExpression(Expression expression) { FieldExpression fieldExpression = new FieldExpression(); //Add already parsed expression to field fieldExpression.SetExpression(expression); //Skip . token NextToken(".", "expression.identifier", '.'); //Parse identifier CurrentToken = TokenStream.NextToken(); fieldExpression.SetIdentifier(CurrentToken.GetValue().ToString()); return(fieldExpression); }
/// <summary> /// Parse FileName /// </summary> /// <returns>Parsed FileName</returns> public FileName ParseFileName() { FileName filename = new FileName(); //Filename NextToken("filename", "filename.ext"); filename.SetName(new PathElement(CurrentToken.GetValue().ToString())); //Period (between filename and extension) NextToken(".", "filename.ext", '.'); //Extension NextToken("extension", "filename.ext"); filename.SetFileExtension(new FileExt(CurrentToken.GetValue().ToString())); return filename; }
/// <summary> /// Parser for Width_HeightAttribute /// </summary> /// <returns>Parsed Width_HeightAttribute</returns> public Width_HeightAttribute ParseWidth_HeightAttribute() { Width_HeightAttribute widthHeightAttribute = new Width_HeightAttribute(); //Get width CurrentToken = TokenStream.NextToken(); widthHeightAttribute.SetWidth(ObjectToInt(CurrentToken.GetValue())); //Skip % token NextToken("%", "@width %height", '%'); //Get height CurrentToken = TokenStream.NextToken(); widthHeightAttribute.SetHeight(ObjectToInt(CurrentToken.GetValue())); return(widthHeightAttribute); }
/// <summary> /// Parse one module /// </summary> /// <param name="module">Module to parse</param> public Module ParseModule() { Module module = new Module(); //Skip module token NextToken("module", "module identifier", "module"); //Parse first the identifier of the module and set it ModuleId moduleIdentifier = ParseModuleId(); module.SetModuleId(moduleIdentifier); //Look for elements like SITE, DEF, etc while (TokenStream.HasNext()) { if (MatchValue(TokenStream.Peek(1).GetValue().ToString(), Waebric.WaebricKeyword.MODULE.ToString())) { //New module found, raise exception, only one module per file allowed throw new UnexpectedToken("Unexpected token, found second module definition:", "module", TokenStream.Peek(1).GetLine()); } CurrentToken = TokenStream.NextToken(); //Check for different elements which may appear in a module if (MatchValue(CurrentToken.GetValue().ToString(), Waebric.WaebricKeyword.DEF.ToString())) { //Function definition found module.AddFunctionDefinition(functionParser.ParseFunctionDefinition()); } else if (MatchValue(CurrentToken.GetValue().ToString(), Waebric.WaebricKeyword.SITE.ToString())) { //Site definition found, call siteparser module.AddSite(siteParser.ParseSite()); } else if (MatchValue(CurrentToken.GetValue().ToString(), Waebric.WaebricKeyword.IMPORT.ToString())) { //Imports found module.AddImport(ParseImport()); } else { //Exception throw new UnexpectedToken("Unexpected token found:", CurrentToken.GetValue().ToString(), CurrentToken.GetLine()); } } return(module); }
/// <summary> /// Parser for VarBindAssignment /// </summary> /// <returns>Parsed VarBindAssignment</returns> public VarBindAssignment ParseVarBindAssignment() { VarBindAssignment varBindAssignment = new VarBindAssignment(); //Parse identifier CurrentToken = TokenStream.NextToken(); varBindAssignment.SetIdentifier(CurrentToken.GetValue().ToString()); //Skip = token NextToken("=", "identifier = expression;", '='); //Parse expression varBindAssignment.SetExpression(expressionParser.ParseExpression()); //Skip ; token NextToken(";", "identifier = expression;", ';'); return(varBindAssignment); }
/// <summary> /// Parse Directory /// </summary> /// <returns>Parsed Directory</returns> public Directory ParseDirectory() { Directory directory = new Directory(); //Parse path elements while (TokenStream.HasNext()) { if(TokenStream.Peek(2).GetValue().ToString() == ".") { //End of directory, filename starts here break; } PathElement element = new PathElement(); NextToken("directory", "directory/filename.ext", TokenType.IDENTIFIER); element.SetPathElement(CurrentToken.GetValue().ToString()); directory.AddDirectoryElement(element); //Skip / or \ NextToken("/ or \\", "directory/FileName.ext", TokenType.SYMBOL); } return directory; }
/// <summary> /// Parser for Attribute /// </summary> /// <returns>Parsed Attribute</returns> public Attribute ParseAttribute() { CurrentToken = TokenStream.NextToken(); Attribute attribute = null; switch (CurrentToken.GetValue().ToString()) { //Determine different types of attributes case "#": //Id attribute attribute = ParseIdAttribute(); break; case ".": //Class attribute attribute = ParseClassAttribute(); break; case "$": //Name attribute attribute = ParseNameAttribute(); break; case ":": //Type attribute attribute = ParseTypeAttribute(); break; case "@": //Width_(Height) attribute if (TokenStream.Peek(2).GetValue().ToString() == "%") { //Width-Height attribute attribute = ParseWidth_HeightAttribute(); } else { //Only width attribute attribute = ParseWidthAttribute(); } break; default: throw new UnexpectedToken("Expected attribute, but found:", CurrentToken.GetValue().ToString(), CurrentToken.GetLine()); } return(attribute); }
/// <summary> /// Parser for Embedding /// </summary> /// <returns>Parsed Embedding</returns> public Embedding ParseEmbedding() { //Get iterator for specific embedding if (TokenStream.Peek(1).GetType() == TokenType.EMBEDDING) { //Store embedding in internal tokeniterator to parse internally CurrentToken = TokenStream.NextToken(); EmbeddingTokenStream = ((EmbeddingToken)CurrentToken).GetTokenIterator(); } else { //Raise exception throw new UnexpectedToken("Embedding expected, but found:", CurrentToken.GetValue().ToString(), CurrentToken.GetLine()); } //Let's parse embedding Embedding embedding = new Embedding(); embedding.SetPreText(ParsePreText()); embedding.SetEmbed(ParseEmbed()); embedding.SetTextTail(ParseTextTail()); return(embedding); }