/// <summary> /// Visits the type. /// </summary> /// <param name="type">Type</param> public void Visit(ref PBaseType type) { if (base.TokenStream.Done || (base.TokenStream.Peek().Type != TokenType.MachineDecl && base.TokenStream.Peek().Type != TokenType.Int && base.TokenStream.Peek().Type != TokenType.Bool && base.TokenStream.Peek().Type != TokenType.Seq && base.TokenStream.Peek().Type != TokenType.Map && base.TokenStream.Peek().Type != TokenType.Any && base.TokenStream.Peek().Type != TokenType.EventDecl && base.TokenStream.Peek().Type != TokenType.LeftParenthesis)) { throw new ParsingException("Expected type.", new List<TokenType> { TokenType.MachineDecl, TokenType.Int, TokenType.Bool, TokenType.Seq, TokenType.Map, TokenType.Any, TokenType.EventDecl }); } if (base.TokenStream.Peek().Type == TokenType.MachineDecl) { base.TokenStream.Swap(new Token(new TextUnit("MachineId", base.TokenStream. Peek().TextUnit.Line), TokenType.MachineDecl)); type = new PBaseType(PType.Machine); type.TypeTokens.Add(base.TokenStream.Peek()); } else if (base.TokenStream.Peek().Type == TokenType.Int) { type = new PBaseType(PType.Int); type.TypeTokens.Add(base.TokenStream.Peek()); } else if (base.TokenStream.Peek().Type == TokenType.Bool) { type = new PBaseType(PType.Bool); type.TypeTokens.Add(base.TokenStream.Peek()); } else if (base.TokenStream.Peek().Type == TokenType.Any) { type = new PBaseType(PType.Any); type.TypeTokens.Add(base.TokenStream.Peek()); } else if (base.TokenStream.Peek().Type == TokenType.EventDecl) { type = new PBaseType(PType.Event); type.TypeTokens.Add(base.TokenStream.Peek()); } else if (base.TokenStream.Peek().Type == TokenType.Seq) { var seqType = new PSeqType(); new SeqTypeIdentifierVisitor(base.TokenStream).Visit(ref seqType); type = seqType; } else if (base.TokenStream.Peek().Type == TokenType.Map) { var mapType = new PMapType(); new MapTypeIdentifierVisitor(base.TokenStream).Visit(ref mapType); type = mapType; } else if (base.TokenStream.Peek().Type == TokenType.LeftParenthesis) { var tupleType = new PTupleType(); new TupleTypeIdentifierVisitor(base.TokenStream).Visit(ref tupleType); type = tupleType; } base.TokenStream.Index++; base.TokenStream.SkipWhiteSpaceAndCommentTokens(); }
/// <summary> /// Visits the syntax node /// </summary> /// <param name="type">Type</param> public void Visit(ref PTupleType type) { type.TypeTokens.Add(base.TokenStream.Peek()); base.TokenStream.Index++; base.TokenStream.SkipWhiteSpaceAndCommentTokens(); if (base.TokenStream.Done || (base.TokenStream.Peek().Type != TokenType.MachineDecl && base.TokenStream.Peek().Type != TokenType.Int && base.TokenStream.Peek().Type != TokenType.Bool && base.TokenStream.Peek().Type != TokenType.Seq && base.TokenStream.Peek().Type != TokenType.Map && base.TokenStream.Peek().Type != TokenType.Any && base.TokenStream.Peek().Type != TokenType.EventDecl && base.TokenStream.Peek().Type != TokenType.LeftParenthesis && base.TokenStream.Peek().Type != TokenType.Identifier)) { throw new ParsingException("Expected type.", new List<TokenType> { TokenType.MachineDecl, TokenType.Int, TokenType.Bool, TokenType.Seq, TokenType.Map, TokenType.Any, TokenType.EventDecl }); } bool isNamed = false; bool expectsName = false; if (base.TokenStream.Peek().Type == TokenType.Identifier) { isNamed = true; expectsName = true; } bool expectsComma = false; while (!base.TokenStream.Done && base.TokenStream.Peek().Type != TokenType.RightParenthesis) { if ((!expectsComma && !expectsName && base.TokenStream.Peek().Type != TokenType.MachineDecl && base.TokenStream.Peek().Type != TokenType.Int && base.TokenStream.Peek().Type != TokenType.Bool && base.TokenStream.Peek().Type != TokenType.Seq && base.TokenStream.Peek().Type != TokenType.Map && base.TokenStream.Peek().Type != TokenType.Any && base.TokenStream.Peek().Type != TokenType.EventDecl && base.TokenStream.Peek().Type != TokenType.LeftParenthesis) || (expectsName && base.TokenStream.Peek().Type != TokenType.Identifier) || (expectsComma && base.TokenStream.Peek().Type != TokenType.Comma)) { break; } if (base.TokenStream.Peek().Type == TokenType.MachineDecl || base.TokenStream.Peek().Type == TokenType.Int || base.TokenStream.Peek().Type == TokenType.Bool || base.TokenStream.Peek().Type == TokenType.Seq || base.TokenStream.Peek().Type == TokenType.Map || base.TokenStream.Peek().Type == TokenType.Any || base.TokenStream.Peek().Type == TokenType.EventDecl || base.TokenStream.Peek().Type == TokenType.LeftParenthesis) { PBaseType tupleType = null; new TypeIdentifierVisitor(base.TokenStream).Visit(ref tupleType); type.TupleTypes.Add(tupleType); expectsComma = true; } else if (base.TokenStream.Peek().Type == TokenType.Identifier) { type.NameTokens.Add(base.TokenStream.Peek()); base.TokenStream.Index++; base.TokenStream.SkipWhiteSpaceAndCommentTokens(); if (base.TokenStream.Done || (base.TokenStream.Peek().Type != TokenType.Colon)) { throw new ParsingException("Expected \":\".", new List<TokenType> { TokenType.Colon }); } base.TokenStream.Index++; base.TokenStream.SkipWhiteSpaceAndCommentTokens(); expectsName = false; } else if (base.TokenStream.Peek().Type == TokenType.Comma) { type.TypeTokens.Add(base.TokenStream.Peek()); base.TokenStream.Index++; base.TokenStream.SkipWhiteSpaceAndCommentTokens(); expectsComma = false; if (isNamed) { expectsName = true; } } } if (base.TokenStream.Done || base.TokenStream.Peek().Type != TokenType.RightParenthesis) { throw new ParsingException("Expected \")\".", new List<TokenType> { TokenType.RightParenthesis }); } type.TypeTokens.Add(base.TokenStream.Peek()); }