How to use parseIdent method of ast Package

Best Syzkaller code snippet using ast.parseIdent

statement_parser.go

Source:statement_parser.go Github

copy

Full Screen

...10 }11 if !p.expectPeek(token.IDENT) {12 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "IDENT"))13 }14 i.Name = p.parseIdent()15 if !p.peekTokenIs(token.SEMICOLON) {16 return nil, errors.WithStack(MissingSemicolon(p.curToken))17 }18 i.Meta.Trailing = p.trailing()19 p.nextToken() // point to SEMICOLON20 return i, nil21}22func (p *Parser) parseIncludeStatement() (ast.Statement, error) {23 i := &ast.IncludeStatement{24 Meta: p.curToken,25 }26 if !p.expectPeek(token.STRING) {27 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "STRING"))28 }29 i.Module = p.parseString()30 i.Meta.Trailing = p.trailing()31 // Semicolons are actually not required at the end of include lines32 // either works on fastly.33 if p.peekTokenIs(token.SEMICOLON) {34 p.nextToken() // point to SEMICOLON35 }36 return i, nil37}38func (p *Parser) parseBlockStatement() (*ast.BlockStatement, error) {39 // Note: block statement is used for declaration/statement inside like subroutine, if, elseif, else40 // on start this statement, current token must point start of LEFT_BRACE41 // and after on end this statement, current token must poinrt end of RIGHT_BRACE42 b := &ast.BlockStatement{43 Meta: p.curToken,44 Statements: []ast.Statement{},45 }46 for !p.peekTokenIs(token.RIGHT_BRACE) {47 var stmt ast.Statement48 var err error49 p.nextToken() // point to statement50 switch p.curToken.Token.Type {51 // https://github.com/ysugimoto/falco/issues/1752 // VCL accepts block syntax:53 // ```54 // sub vcl_recv {55 // {56 // log "recv";57 // }58 // }59 // ```60 case token.LEFT_BRACE:61 stmt, err = p.parseBlockStatement()62 case token.SET:63 stmt, err = p.parseSetStatement()64 case token.UNSET:65 stmt, err = p.parseUnsetStatement()66 case token.REMOVE:67 stmt, err = p.parseRemoveStatement()68 case token.ADD:69 stmt, err = p.parseAddStatement()70 case token.CALL:71 stmt, err = p.parseCallStatement()72 case token.DECLARE:73 stmt, err = p.parseDeclareStatement()74 case token.ERROR:75 stmt, err = p.parseErrorStatement()76 case token.ESI:77 stmt, err = p.parseEsiStatement()78 case token.LOG:79 stmt, err = p.parseLogStatement()80 case token.RESTART:81 stmt, err = p.parseRestartStatement()82 case token.RETURN:83 stmt, err = p.parseReturnStatement()84 case token.SYNTHETIC:85 stmt, err = p.parseSyntheticStatement()86 case token.SYNTHETIC_BASE64:87 stmt, err = p.parseSyntheticBase64Statement()88 case token.IF:89 stmt, err = p.parseIfStatement()90 case token.GOTO:91 stmt, err = p.parseGotoStatement()92 case token.IDENT:93 // Check if the current ident is a function call94 if p.peekTokenIs(token.LEFT_PAREN) {95 stmt, err = p.parseFunctionCall()96 } else {97 // Could be a goto destination98 stmt, err = p.parseGotoDestination()99 }100 default:101 err = UnexpectedToken(p.peekToken)102 }103 if err != nil {104 return nil, errors.WithStack(err)105 }106 b.Statements = append(b.Statements, stmt)107 }108 b.Meta.Trailing = p.trailing()109 p.nextToken() // point to RIGHT_BRACE110 // RIGHT_BRACE leading comments are block infix comments111 swapLeadingInfix(p.curToken, b.Meta)112 return b, nil113}114// nolint: dupl115func (p *Parser) parseSetStatement() (*ast.SetStatement, error) {116 stmt := &ast.SetStatement{117 Meta: p.curToken,118 }119 if !p.expectPeek(token.IDENT) {120 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "IDENT"))121 }122 stmt.Ident = p.parseIdent()123 if !isAssignmentOperator(p.peekToken.Token) {124 return nil, errors.WithStack(UnexpectedToken(p.peekToken, assignmentOperatorLiterals...))125 }126 p.nextToken() // point to assignment operator127 swapLeadingTrailing(p.curToken, stmt.Ident.Meta)128 stmt.Operator = &ast.Operator{129 Meta: p.curToken,130 Operator: p.curToken.Token.Literal,131 }132 p.nextToken() // point to right expression start133 exp, err := p.parseExpression(LOWEST)134 if err != nil {135 return nil, errors.WithStack(err)136 }137 stmt.Value = exp138 if !p.peekTokenIs(token.SEMICOLON) {139 return nil, errors.WithStack(MissingSemicolon(p.curToken))140 }141 stmt.Meta.Trailing = p.trailing()142 p.nextToken() // point to SEMICOLON143 return stmt, nil144}145func (p *Parser) parseUnsetStatement() (*ast.UnsetStatement, error) {146 stmt := &ast.UnsetStatement{147 Meta: p.curToken,148 }149 if !p.expectPeek(token.IDENT) {150 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "IDENT"))151 }152 stmt.Ident = p.parseIdent()153 if !p.peekTokenIs(token.SEMICOLON) {154 return nil, errors.WithStack(MissingSemicolon(p.curToken))155 }156 stmt.Meta.Trailing = p.trailing()157 p.nextToken() // point to SEMICOLON158 return stmt, nil159}160func (p *Parser) parseRemoveStatement() (*ast.RemoveStatement, error) {161 stmt := &ast.RemoveStatement{162 Meta: p.curToken,163 }164 if !p.expectPeek(token.IDENT) {165 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "IDENT"))166 }167 stmt.Ident = p.parseIdent()168 if !p.peekTokenIs(token.SEMICOLON) {169 return nil, errors.WithStack(MissingSemicolon(p.curToken))170 }171 stmt.Meta.Trailing = p.trailing()172 p.nextToken() // point to SEMICOLON173 return stmt, nil174}175// nolint: dupl176func (p *Parser) parseAddStatement() (*ast.AddStatement, error) {177 stmt := &ast.AddStatement{178 Meta: p.curToken,179 }180 if !p.expectPeek(token.IDENT) {181 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "IDENT"))182 }183 stmt.Ident = p.parseIdent()184 if !isAssignmentOperator(p.peekToken.Token) {185 return nil, errors.WithStack(UnexpectedToken(p.peekToken, assignmentOperatorLiterals...))186 }187 p.nextToken() // pojnt to assignment operator188 swapLeadingTrailing(p.curToken, stmt.Ident.Meta)189 stmt.Operator = &ast.Operator{190 Meta: p.curToken,191 Operator: p.curToken.Token.Literal,192 }193 p.nextToken() // start expression token194 exp, err := p.parseExpression(LOWEST)195 if err != nil {196 return nil, errors.WithStack(err)197 }198 stmt.Value = exp199 if !p.peekTokenIs(token.SEMICOLON) {200 return nil, errors.WithStack(MissingSemicolon(p.curToken))201 }202 stmt.Meta.Trailing = p.trailing()203 p.nextToken() // point to SEMICOLON204 return stmt, nil205}206func (p *Parser) parseCallStatement() (*ast.CallStatement, error) {207 stmt := &ast.CallStatement{208 Meta: p.curToken,209 }210 if !p.expectPeek(token.IDENT) {211 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "IDENT"))212 }213 stmt.Subroutine = p.parseIdent()214 // Parse functions with ()215 if p.peekTokenIs(token.LEFT_PAREN) {216 p.nextToken() // point to the token to check if it is RIGHT_PAREN217 if !p.peekTokenIs(token.RIGHT_PAREN) {218 return nil, errors.WithStack(UnexpectedToken(p.curToken))219 }220 p.nextToken() // point to RIGHT_PAREN221 }222 if !p.peekTokenIs(token.SEMICOLON) {223 return nil, errors.WithStack(MissingSemicolon(p.curToken))224 }225 stmt.Meta.Trailing = p.trailing()226 p.nextToken() // point to SEMICOLON227 return stmt, nil228}229func (p *Parser) parseDeclareStatement() (*ast.DeclareStatement, error) {230 stmt := &ast.DeclareStatement{231 Meta: p.curToken,232 }233 // Declare Syntax is declare [IDENT:"local"] [IDENT:variable name] [IDENT:VCL type]234 if !p.expectPeek(token.IDENT) {235 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "IDENT"))236 }237 if p.curToken.Token.Literal != "local" {238 return nil, errors.WithStack(UnexpectedToken(p.curToken, "local"))239 }240 if !p.expectPeek(token.IDENT) {241 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "IDENT"))242 }243 stmt.Name = p.parseIdent()244 if !p.expectPeek(token.IDENT) {245 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "IDENT"))246 }247 stmt.ValueType = p.parseIdent()248 if !p.peekTokenIs(token.SEMICOLON) {249 return nil, errors.WithStack(MissingSemicolon(p.curToken))250 }251 stmt.Meta.Trailing = p.trailing()252 p.nextToken() // point to SEMICOLON253 return stmt, nil254}255func (p *Parser) parseErrorStatement() (*ast.ErrorStatement, error) {256 stmt := &ast.ErrorStatement{257 Meta: p.curToken,258 }259 // error code token must be ident or integer260 var err error261 switch p.peekToken.Token.Type {262 case token.INT:263 p.nextToken()264 stmt.Code, err = p.parseInteger()265 case token.IDENT:266 p.nextToken()267 if p.peekTokenIs(token.LEFT_PAREN) {268 i := p.parseIdent()269 p.nextToken()270 stmt.Code, err = p.parseFunctionCallExpression(i)271 } else {272 stmt.Code = p.parseIdent()273 }274 default:275 err = UnexpectedToken(p.peekToken)276 }277 if err != nil {278 return nil, errors.WithStack(err)279 }280 // Optional expression, error statement argument281 if !p.peekTokenIs(token.SEMICOLON) {282 p.nextToken()283 stmt.Argument, err = p.parseExpression(LOWEST)284 if err != nil {285 return nil, errors.WithStack(err)286 }287 }288 if !p.peekTokenIs(token.SEMICOLON) {289 return nil, errors.WithStack(MissingSemicolon(p.curToken))290 }291 stmt.Meta.Trailing = p.trailing()292 p.nextToken() // point to SEMICOLON293 return stmt, nil294}295func (p *Parser) parseEsiStatement() (*ast.EsiStatement, error) {296 stmt := &ast.EsiStatement{297 Meta: p.curToken,298 }299 if !p.peekTokenIs(token.SEMICOLON) {300 return nil, errors.WithStack(MissingSemicolon(p.curToken))301 }302 stmt.Meta.Trailing = p.trailing()303 p.nextToken() // point to SEMICOLON304 return stmt, nil305}306func (p *Parser) parseLogStatement() (*ast.LogStatement, error) {307 stmt := &ast.LogStatement{308 Meta: p.curToken,309 }310 p.nextToken() // point to log value expression311 value, err := p.parseExpression(LOWEST)312 if err != nil {313 return nil, errors.WithStack(err)314 }315 stmt.Value = value316 if !p.peekTokenIs(token.SEMICOLON) {317 return nil, errors.WithStack(MissingSemicolon(p.curToken))318 }319 stmt.Meta.Trailing = p.trailing()320 p.nextToken() // point to SEMICOLON321 return stmt, nil322}323func (p *Parser) parseRestartStatement() (*ast.RestartStatement, error) {324 stmt := &ast.RestartStatement{325 Meta: p.curToken,326 }327 if !p.peekTokenIs(token.SEMICOLON) {328 return nil, errors.WithStack(MissingSemicolon(p.curToken))329 }330 stmt.Meta.Trailing = p.trailing()331 p.nextToken() // point to SEMICOLON332 return stmt, nil333}334func (p *Parser) parseReturnStatement() (*ast.ReturnStatement, error) {335 stmt := &ast.ReturnStatement{336 Meta: p.curToken,337 HasParenthesis: false,338 }339 // return statement may not have argument340 // https://developer.fastly.com/reference/vcl/statements/return/341 if p.peekTokenIs(token.SEMICOLON) {342 stmt.Meta.Trailing = p.trailing()343 p.nextToken() // point to SEMICOLON344 return stmt, nil345 }346 hasLeftParen := p.peekTokenIs(token.LEFT_PAREN)347 if hasLeftParen {348 stmt.HasParenthesis = true349 p.nextToken() // point to expression350 }351 p.nextToken()352 expression, err := p.parseExpression(LOWEST)353 if err != nil {354 return nil, errors.WithStack(err)355 }356 stmt.ReturnExpression = &expression357 hasRightParen := p.peekTokenIs(token.RIGHT_PAREN)358 if hasRightParen {359 p.nextToken() // point to condition expression360 }361 if hasLeftParen != hasRightParen {362 return nil, errors.WithStack(&ParseError{363 Token: p.curToken.Token,364 Message: "Parenthesis missmatch",365 })366 }367 swapLeadingTrailing(p.curToken, (*stmt.ReturnExpression).GetMeta())368 if !p.peekTokenIs(token.SEMICOLON) {369 return nil, errors.WithStack(MissingSemicolon(p.curToken))370 }371 stmt.Meta.Trailing = p.trailing()372 p.nextToken() // point to SEMICOLON373 return stmt, nil374}375func (p *Parser) parseSyntheticStatement() (*ast.SyntheticStatement, error) {376 stmt := &ast.SyntheticStatement{377 Meta: p.curToken,378 }379 p.nextToken() // point to synthetic value expression380 value, err := p.parseExpression(LOWEST)381 if err != nil {382 return nil, errors.WithStack(err)383 }384 stmt.Value = value385 if !p.peekTokenIs(token.SEMICOLON) {386 return nil, errors.WithStack(MissingSemicolon(p.curToken))387 }388 stmt.Meta.Trailing = p.trailing()389 p.nextToken() // point to SEMICOLON390 return stmt, nil391}392func (p *Parser) parseSyntheticBase64Statement() (*ast.SyntheticBase64Statement, error) {393 stmt := &ast.SyntheticBase64Statement{394 Meta: p.curToken,395 }396 p.nextToken()397 value, err := p.parseExpression(LOWEST)398 if err != nil {399 return nil, errors.WithStack(err)400 }401 stmt.Value = value402 if !p.peekTokenIs(token.SEMICOLON) {403 return nil, errors.WithStack(MissingSemicolon(p.curToken))404 }405 stmt.Meta.Trailing = p.trailing()406 p.nextToken() // point to SEMICOLON407 return stmt, nil408}409// nolint: gocognit410func (p *Parser) parseIfStatement() (*ast.IfStatement, error) {411 stmt := &ast.IfStatement{412 Meta: p.curToken,413 }414 if !p.expectPeek(token.LEFT_PAREN) {415 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "LEFT_PAREN"))416 }417 p.nextToken() // point to condition expression418 cond, err := p.parseExpression(LOWEST)419 if err != nil {420 return nil, errors.WithStack(err)421 }422 stmt.Condition = cond423 if !p.expectPeek(token.RIGHT_PAREN) {424 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "RIGHT_PAREN"))425 }426 if !p.expectPeek(token.LEFT_BRACE) {427 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "LEFT_BRACE"))428 }429 // parse Consequence block430 stmt.Consequence, err = p.parseBlockStatement()431 if err != nil {432 return nil, errors.WithStack(err)433 }434 // cursor must be on RIGHT_BRACE435 // If statement may have some "else if" or "else" as another/alternative statement436 for {437 switch p.peekToken.Token.Type {438 case token.ELSE: // else439 p.nextToken() // point to ELSE440 // If more peek token is IF, it should be "else if"441 if p.peekTokenIs(token.IF) { // else if442 p.nextToken() // point to IF443 another, err := p.parseAnotherIfStatement()444 if err != nil {445 return nil, errors.WithStack(err)446 }447 stmt.Another = append(stmt.Another, another)448 continue449 }450 // Otherwise, it is else statement. next token must be LEFT_BRACE451 if !p.expectPeek(token.LEFT_BRACE) {452 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "LEFT_BRACE"))453 }454 stmt.Alternative, err = p.parseBlockStatement()455 if err != nil {456 return nil, errors.WithStack(err)457 }458 // exit for loop459 goto FINISH460 // Note: VCL could define "else if" statement with "elseif", "elsif" keyword461 case token.ELSEIF, token.ELSIF: // elseif, elsif462 p.nextToken() // point to ELSEIF/ELSIF463 another, err := p.parseAnotherIfStatement()464 if err != nil {465 return nil, errors.WithStack(err)466 }467 stmt.Another = append(stmt.Another, another)468 continue469 }470 // If not match, if statement does not have another/alternative statement471 goto FINISH472 }473FINISH:474 return stmt, nil475}476// AnotherIfStatement is similar to IfStatement but is not culious about alternative.477func (p *Parser) parseAnotherIfStatement() (*ast.IfStatement, error) {478 stmt := &ast.IfStatement{479 Meta: p.curToken,480 }481 if !p.expectPeek(token.LEFT_PAREN) {482 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "LEFT_PAREN"))483 }484 p.nextToken() // point to condition expression485 cond, err := p.parseExpression(LOWEST)486 if err != nil {487 return nil, errors.WithStack(err)488 }489 stmt.Condition = cond490 if !p.expectPeek(token.RIGHT_PAREN) {491 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "RIGHT_PAREN"))492 }493 if !p.expectPeek(token.LEFT_BRACE) {494 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "LEFT_BRACE"))495 }496 // parse Consequence block497 stmt.Consequence, err = p.parseBlockStatement()498 if err != nil {499 return nil, errors.WithStack(err)500 }501 // cursor must be on RIGHT_BRACE502 return stmt, nil503}504func (p *Parser) parseGotoStatement() (*ast.GotoStatement, error) {505 stmt := &ast.GotoStatement{506 Meta: p.curToken,507 }508 if !p.expectPeek(token.IDENT) {509 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "IDENT"))510 }511 stmt.Destination = p.parseIdent()512 if !p.peekTokenIs(token.SEMICOLON) {513 return nil, errors.WithStack(MissingSemicolon(p.curToken))514 }515 stmt.Meta.Trailing = p.trailing()516 p.nextToken() // point to SEMICOLON517 return stmt, nil518}519func (p *Parser) parseGotoDestination() (*ast.GotoDestinationStatement, error) {520 if !isGotoDestination(p.curToken.Token) {521 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "IDENT"))522 }523 stmt := &ast.GotoDestinationStatement{524 Meta: p.curToken,525 }526 stmt.Name = p.parseIdent()527 stmt.Meta.Trailing = p.trailing()528 return stmt, nil529}530func (p *Parser) parseFunctionCall() (*ast.FunctionCallStatement, error) {531 stmt := &ast.FunctionCallStatement{532 Meta: p.curToken,533 Function: p.parseIdent(),534 }535 p.nextToken() // point to LEFT_PAREN536 args, err := p.parseFunctionArgumentExpressions()537 if err != nil {538 return nil, errors.WithStack(err)539 }540 stmt.Arguments = args541 if !p.peekTokenIs(token.SEMICOLON) {542 return nil, errors.WithStack(MissingSemicolon(p.curToken))543 }544 stmt.Meta.Trailing = p.trailing()545 p.nextToken() // point to SEMICOLON546 return stmt, nil547}...

Full Screen

Full Screen

declaration_parser.go

Source:declaration_parser.go Github

copy

Full Screen

...10 }11 if !p.expectPeek(token.IDENT) {12 return nil, errors.WithStack(UnexpectedToken(p.peekToken, token.IDENT))13 }14 acl.Name = p.parseIdent()15 if !p.expectPeek(token.LEFT_BRACE) {16 return nil, errors.WithStack(UnexpectedToken(p.peekToken, token.LEFT_BRACE))17 }18 swapLeadingTrailing(p.curToken, acl.Name.Meta)19 for !p.peekTokenIs(token.RIGHT_BRACE) {20 p.nextToken() // point to CIDR start21 cidr, err := p.parseAclCidr()22 if err != nil {23 return nil, errors.WithStack(err)24 } else if cidr != nil {25 acl.CIDRs = append(acl.CIDRs, cidr)26 }27 }28 acl.Meta.Trailing = p.trailing()29 p.nextToken() // point to RIGHT BRACE30 // RIGHT_BRACE leading comments are ACL infix comments31 swapLeadingInfix(p.curToken, acl.Meta)32 return acl, nil33}34func (p *Parser) parseAclCidr() (*ast.AclCidr, error) {35 cidr := &ast.AclCidr{36 Meta: p.curToken,37 }38 // Set inverse if "!" token exists39 var err error40 if p.curTokenIs(token.NOT) {41 cidr.Inverse = &ast.Boolean{42 Meta: clearComments(p.curToken),43 Value: true,44 }45 p.nextToken() // point to IP token46 }47 if !p.curTokenIs(token.STRING) {48 return nil, errors.WithStack(UnexpectedToken(p.peekToken, token.STRING))49 }50 cidr.IP = p.parseIP()51 cidr.IP.Meta = clearComments(cidr.IP.Meta)52 // If SLASH token is found on peek token, need to parse CIDR mask bit53 if p.peekTokenIs(token.SLASH) {54 p.nextToken() // point to SLASH55 if !p.expectPeek(token.INT) {56 return nil, errors.WithStack(UnexpectedToken(p.peekToken, token.INT))57 }58 cidr.Mask, err = p.parseInteger()59 if err != nil {60 return nil, errors.WithStack(err)61 }62 }63 if !p.peekTokenIs(token.SEMICOLON) {64 return nil, errors.WithStack(MissingSemicolon(p.curToken))65 }66 cidr.Meta.Trailing = p.trailing()67 p.nextToken() // point to semicolon68 return cidr, nil69}70func (p *Parser) parseBackendDeclaration() (*ast.BackendDeclaration, error) {71 b := &ast.BackendDeclaration{72 Meta: p.curToken,73 }74 if !p.expectPeek(token.IDENT) {75 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "IDENT"))76 }77 b.Name = p.parseIdent()78 if !p.expectPeek(token.LEFT_BRACE) {79 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "LEFT_BRACE"))80 }81 swapLeadingTrailing(p.curToken, b.Name.Meta)82 for !p.peekTokenIs(token.RIGHT_BRACE) {83 prop, err := p.parseBackendProperty()84 if err != nil {85 return nil, errors.WithStack(err)86 }87 b.Properties = append(b.Properties, prop)88 }89 if !p.peekTokenIs(token.RIGHT_BRACE) {90 return nil, errors.WithStack(UnexpectedToken(p.curToken, "RIGHT_BRACE"))91 }92 swapLeadingInfix(p.peekToken, b.Meta)93 b.Meta.Trailing = p.trailing()94 p.nextToken()95 return b, nil96}97func (p *Parser) parseBackendProperty() (*ast.BackendProperty, error) {98 if !p.expectPeek(token.DOT) {99 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "DOT"))100 }101 prop := &ast.BackendProperty{102 Meta: p.curToken,103 }104 if !p.expectPeek(token.IDENT) {105 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "IDENT"))106 }107 prop.Key = p.parseIdent()108 if !p.expectPeek(token.ASSIGN) {109 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "ASSIGN"))110 }111 swapLeadingTrailing(p.curToken, prop.Key.Meta)112 p.nextToken() // point to right token113 // When current token is "{", property key should be ".probe"114 if p.curTokenIs(token.LEFT_BRACE) {115 probe := &ast.BackendProbeObject{116 Meta: p.curToken,117 }118 for !p.peekTokenIs(token.RIGHT_BRACE) {119 pp, err := p.parseBackendProperty()120 if err != nil {121 return nil, errors.WithStack(err)122 }123 probe.Values = append(probe.Values, pp)124 }125 probe.Meta.Trailing = p.trailing()126 p.nextToken() // point to RIGHT_BRACE127 swapLeadingInfix(p.curToken, probe.Meta)128 prop.Value = probe129 return prop, nil130 }131 // Otherwise, parse expression132 exp, err := p.parseExpression(LOWEST)133 if err != nil {134 return nil, errors.WithStack(err)135 }136 prop.Value = exp137 if !p.peekTokenIs(token.SEMICOLON) {138 return nil, errors.WithStack(MissingSemicolon(p.curToken))139 }140 prop.Meta.Trailing = p.trailing()141 p.nextToken() // point to SEMICOLON142 return prop, nil143}144func (p *Parser) parseDirectorDeclaration() (*ast.DirectorDeclaration, error) {145 d := &ast.DirectorDeclaration{146 Meta: p.curToken,147 }148 // director name149 if !p.expectPeek(token.IDENT) {150 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "IDENT"))151 }152 d.Name = p.parseIdent()153 // director type154 if !p.expectPeek(token.IDENT) {155 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "IDENT"))156 }157 d.DirectorType = p.parseIdent()158 if !p.expectPeek(token.LEFT_BRACE) {159 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "LEFT_BRACE"))160 }161 swapLeadingTrailing(p.curToken, d.DirectorType.Meta)162 // Parse declaration163 for !p.peekTokenIs(token.RIGHT_BRACE) {164 var prop ast.Expression165 var err error166 switch p.peekToken.Token.Type {167 case token.DOT:168 // single property definition like ".quorum = 10%;"169 p.nextToken()170 prop, err = p.parseDirectorProperty()171 case token.LEFT_BRACE:172 p.nextToken()173 // object definition e.g. { .backend = F_origin_1; .weight = 1; }174 prop, err = p.parseDirectorBackend()175 default:176 err = errors.WithStack(UnexpectedToken(p.peekToken))177 }178 if err != nil {179 return nil, errors.WithStack(err)180 }181 d.Properties = append(d.Properties, prop)182 }183 swapLeadingInfix(p.peekToken, d.Meta)184 d.Meta.Trailing = p.trailing()185 p.nextToken() // point to RIGHT_BRACE186 return d, nil187}188func (p *Parser) parseDirectorProperty() (ast.Expression, error) {189 prop := &ast.DirectorProperty{190 Meta: p.curToken,191 }192 // token may token.BACKEND because backend object has ".backend" property key193 if !p.expectPeek(token.IDENT) && !p.expectPeek(token.BACKEND) {194 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "IDENT"))195 }196 prop.Key = p.parseIdent()197 if !p.expectPeek(token.ASSIGN) {198 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "ASSIGN"))199 }200 swapLeadingTrailing(p.curToken, prop.Key.Meta)201 p.nextToken() // point to expression start token202 exp, err := p.parseExpression(LOWEST)203 if err != nil {204 return nil, errors.WithStack(err)205 }206 prop.Value = exp207 if !p.peekTokenIs(token.SEMICOLON) {208 return nil, errors.WithStack(MissingSemicolon(p.curToken))209 }210 prop.Meta.Trailing = p.trailing()211 p.nextToken() // point to SEMICOLON212 return prop, nil213}214func (p *Parser) parseDirectorBackend() (ast.Expression, error) {215 backend := &ast.DirectorBackendObject{216 Meta: p.curToken,217 }218 for !p.peekTokenIs(token.RIGHT_BRACE) {219 if !p.expectPeek(token.DOT) {220 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "DOT"))221 }222 prop := &ast.DirectorProperty{223 Meta: p.curToken,224 }225 // token may token.BACKEND because backend object has ".backend" property key226 if !p.expectPeek(token.IDENT) && !p.expectPeek(token.BACKEND) {227 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "IDENT"))228 }229 prop.Key = p.parseIdent()230 if !p.expectPeek(token.ASSIGN) {231 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "ASSIGN"))232 }233 swapLeadingTrailing(p.curToken, prop.Key.Meta)234 p.nextToken() // point to expression start token235 exp, err := p.parseExpression(LOWEST)236 if err != nil {237 return nil, errors.WithStack(err)238 }239 prop.Value = exp240 if !p.peekTokenIs(token.SEMICOLON) {241 return nil, errors.WithStack(MissingSemicolon(p.curToken))242 }243 prop.Meta.Trailing = p.trailing()244 p.nextToken() // point to SEMICOLON245 backend.Values = append(backend.Values, prop)246 }247 swapLeadingInfix(p.peekToken, backend.Meta)248 backend.Meta.Trailing = p.trailing()249 p.nextToken() // point to RIGHT_BRACE250 return backend, nil251}252func (p *Parser) parseTableDeclaration() (*ast.TableDeclaration, error) {253 t := &ast.TableDeclaration{254 Meta: p.curToken,255 Properties: []*ast.TableProperty{},256 }257 if !p.expectPeek(token.IDENT) {258 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "IDENT"))259 }260 t.Name = p.parseIdent()261 // Table value type is optional262 if p.peekTokenIs(token.IDENT) {263 p.nextToken()264 t.ValueType = p.parseIdent()265 }266 if !p.expectPeek(token.LEFT_BRACE) {267 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "LEFT_BRACE"))268 }269 if t.ValueType != nil {270 swapLeadingTrailing(p.curToken, t.ValueType.Meta)271 } else {272 swapLeadingTrailing(p.curToken, t.Name.Meta)273 }274 for !p.peekTokenIs(token.RIGHT_BRACE) {275 prop, err := p.parseTableProperty()276 if err != nil {277 return nil, errors.WithStack(err)278 }279 t.Properties = append(t.Properties, prop)280 }281 swapLeadingInfix(p.peekToken, t.Meta)282 t.Meta.Trailing = p.trailing()283 p.nextToken() // point to RIGHT_BRACE284 return t, nil285}286func (p *Parser) parseTableProperty() (*ast.TableProperty, error) {287 if !p.expectPeek(token.STRING) {288 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "STRING"))289 }290 prop := &ast.TableProperty{291 Meta: p.curToken,292 Key: p.parseString(),293 }294 prop.Key.Meta = clearComments(prop.Key.Meta)295 if !p.expectPeek(token.COLON) {296 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "COLON"))297 }298 swapLeadingTrailing(p.curToken, prop.Key.Meta)299 p.nextToken() // point to table value token300 switch p.curToken.Token.Type {301 case token.IDENT:302 prop.Value = p.parseIdent()303 case token.STRING:304 prop.Value = p.parseString()305 case token.ACL, token.BACKEND:306 prop.Value = p.parseIdent()307 case token.TRUE, token.FALSE:308 prop.Value = p.parseBoolean()309 case token.FLOAT:310 if v, err := p.parseFloat(); err != nil {311 return nil, errors.WithStack(err)312 } else {313 prop.Value = v314 }315 case token.INT:316 if v, err := p.parseInteger(); err != nil {317 return nil, errors.WithStack(err)318 } else {319 prop.Value = v320 }321 case token.RTIME:322 if v, err := p.parseRTime(); err != nil {323 return nil, errors.WithStack(err)324 } else {325 prop.Value = v326 }327 default:328 return nil, errors.WithStack(UnexpectedToken(p.curToken))329 }330 // Tailing process: the last table property may not need COLON,331 // so we have to be able to parse these case (COLON exists or not)332 switch p.peekToken.Token.Type {333 case token.COMMA:334 // usual case, user should add trailing comma for east properties :)335 prop.Meta.Trailing = p.trailing()336 prop.HasComma = true337 p.nextToken() // point to COMMA338 case token.RIGHT_BRACE:339 // if peed token is RIGHT_BRACE, it means table declaration end. if also be valid340 // Note that in this case, we could not parse trailing comment. it is parsed as declaration infix comment.341 prop.Meta.Trailing = p.trailing()342 // DO NOT advance token!343 default:344 // Other tokens are invalid345 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "COMMA"))346 }347 return prop, nil348}349func (p *Parser) parseSubroutineDeclaration() (*ast.SubroutineDeclaration, error) {350 s := &ast.SubroutineDeclaration{351 Meta: p.curToken,352 }353 if !p.expectPeek(token.IDENT) {354 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "IDENT"))355 }356 s.Name = p.parseIdent()357 // Custom subroutines might be returning a type358 // https://developer.fastly.com/reference/vcl/subroutines/359 // we dont need to validate the type here, linter will do that later.360 if p.expectPeek(token.IDENT) {361 s.ReturnType = p.parseIdent()362 }363 if !p.expectPeek(token.LEFT_BRACE) {364 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "LEFT_BRACE"))365 }366 swapLeadingTrailing(p.curToken, s.Name.Meta)367 var err error368 if s.Block, err = p.parseBlockStatement(); err != nil {369 return nil, errors.WithStack(err)370 }371 // After block statement is parsed, cursor should point to RIGHT_BRACE, end of block statement372 return s, nil373}374func (p *Parser) parsePenaltyboxDeclaration() (*ast.PenaltyboxDeclaration, error) {375 pb := &ast.PenaltyboxDeclaration{376 Meta: p.curToken,377 }378 if !p.expectPeek(token.IDENT) {379 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "IDENT"))380 }381 pb.Name = p.parseIdent()382 if !p.expectPeek(token.LEFT_BRACE) {383 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "LEFT_BRACE"))384 }385 swapLeadingTrailing(p.curToken, pb.Name.Meta)386 var err error387 if pb.Block, err = p.parseBlockStatement(); err != nil {388 return nil, errors.WithStack(err)389 }390 return pb, nil391}392func (p *Parser) parseRatecounterDeclaration() (*ast.RatecounterDeclaration, error) {393 r := &ast.RatecounterDeclaration{394 Meta: p.curToken,395 }396 if !p.expectPeek(token.IDENT) {397 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "IDENT"))398 }399 r.Name = p.parseIdent()400 if !p.expectPeek(token.LEFT_BRACE) {401 return nil, errors.WithStack(UnexpectedToken(p.peekToken, "LEFT_BRACE"))402 }403 swapLeadingTrailing(p.curToken, r.Name.Meta)404 var err error405 if r.Block, err = p.parseBlockStatement(); err != nil {406 return nil, errors.WithStack(err)407 }408 return r, nil409}...

Full Screen

Full Screen

parser.go

Source:parser.go Github

copy

Full Screen

...58 }59 if tok == token.VAR {60 match(token.VAR)61 v := make([]*ast.Ident, 1)62 v[0] = parseIdent()63 for tok == token.COMMA {64 match(token.COMMA)65 v = append(v, parseIdent())66 }67 match(token.SEMICOLON)68 b.Vars = v69 }70 var p []*ast.ProcDecl71 for tok == token.PROCEDURE {72 p = append(p, parseProc())73 }74 b.Procs = p75 b.Body = parseStmt()76 return b77}78func parseConstDecl() *ast.ConstDecl {79 name := parseIdent()80 match(token.EQL)81 return &ast.ConstDecl{Name: name, Value: parseNumber()}82}83func parseProc() *ast.ProcDecl {84 match(token.PROCEDURE)85 name := parseIdent()86 match(token.SEMICOLON)87 block := parseBlock()88 match(token.SEMICOLON)89 return &ast.ProcDecl{Name: name, Block: block}90}91func parseStmt() ast.Stmt {92 switch tok {93 case token.IDENT:94 return parseAssign()95 case token.CALL:96 return parseCall()97 case token.SEND:98 return parseSend()99 case token.RECV:100 return parseReceive()101 case token.BEGIN:102 return parseBegin()103 case token.IF:104 return parseIf()105 case token.WHILE:106 return parseWhile()107 }108 return nil109}110func parseAssign() *ast.AssignStmt {111 i := parseIdent()112 match(token.BECOMES)113 x := parseExpr()114 return &ast.AssignStmt{Lhs: i, Rhs: x}115}116func parseCall() *ast.CallStmt {117 match(token.CALL)118 return &ast.CallStmt{Proc: parseIdent()}119}120func parseSend() *ast.SendStmt {121 match(token.SEND)122 return &ast.SendStmt{X: parseExpr()}123}124func parseReceive() *ast.ReceiveStmt {125 match(token.RECV)126 return &ast.ReceiveStmt{Name: parseIdent()}127}128func parseBegin() *ast.BeginStmt {129 match(token.BEGIN)130 s := make([]ast.Stmt, 1)131 s[0] = parseStmt()132 for tok == token.SEMICOLON {133 match(token.SEMICOLON)134 s = append(s, parseStmt())135 }136 match(token.END)137 return &ast.BeginStmt{List: s}138}139func parseIf() *ast.IfStmt {140 match(token.IF)141 c := parseCond()142 match(token.THEN)143 s := parseStmt()144 return &ast.IfStmt{Cond: c, Body: s}145}146func parseWhile() *ast.WhileStmt {147 match(token.WHILE)148 c := parseCond()149 match(token.DO)150 s := parseStmt()151 return &ast.WhileStmt{Cond: c, Body: s}152}153func parseCond() ast.Cond {154 if tok == token.ODD {155 match(token.ODD)156 return &ast.OddCond{X: parseExpr()}157 }158 return parseRel()159}160func parseRel() *ast.RelCond {161 x := parseExpr()162 if !tok.IsRelop() {163 expected("relation", text)164 return nil165 }166 op := tok167 next()168 return &ast.RelCond{X: x, Op: op, Y: parseExpr()}169}170func parseExpr() ast.Expr {171 sign := tok172 if sign.IsAddop() {173 next()174 }175 x := parseTerm()176 if sign.IsAddop() {177 x = &ast.UnaryExpr{X: x, Op: sign}178 }179 if tok.IsAddop() {180 op := tok181 next()182 return &ast.BinaryExpr{X: x, Op: op, Y: parseExpr()}183 }184 return x185}186func parseTerm() ast.Expr {187 x := parseFact()188 if tok.IsMulop() {189 op := tok190 next()191 return &ast.BinaryExpr{X: x, Op: op, Y: parseTerm()}192 }193 return x194}195func parseFact() ast.Expr {196 switch tok {197 case token.IDENT:198 return parseIdent()199 case token.NUMBER:200 return parseNumber()201 case token.LPAREN:202 match(token.LPAREN)203 x := parseExpr()204 match(token.RPARAN)205 return x206 default:207 expected("expression", text)208 }209 return nil210}211func parseIdent() *ast.Ident {212 if tok != token.IDENT {213 expected("identifier", text)214 }215 i := &ast.Ident{Name: text}216 next()217 return i218}219func parseNumber() *ast.Number {220 if tok != token.NUMBER {221 expected("number", text)222 }223 n := &ast.Number{Value: text}224 next()225 return n...

Full Screen

Full Screen

parseIdent

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fset := token.NewFileSet()4 f, err := parser.ParseFile(fset, "1.go", nil, 0)5 if err != nil {6 fmt.Println(err)7 }8 ast.Print(fset, f)9}10import (11func main() {12 fset := token.NewFileSet()13 f, err := parser.ParseFile(fset, "1.go", nil, 0)14 if err != nil {15 fmt.Println(err)16 }17 ast.Print(fset, f)18}

Full Screen

Full Screen

parseIdent

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fset := token.NewFileSet()4 f, err := parser.ParseFile(fset, "1.go", nil, 0)5 if err != nil {6 panic(err)7 }8 ast.Inspect(f, func(n ast.Node) bool {9 switch x := n.(type) {10 fmt.Println(x.Name)11 }12 })13}

Full Screen

Full Screen

parseIdent

Using AI Code Generation

copy

Full Screen

1import "go/ast"2import "fmt"3func main() {4 var ident = ast.ParseIdent(str)5 fmt.Println(ident)6}7&{hello 0}8Recommended Posts: Go | ParseSelectorExpr() method9Go | ParseStmt() method10Go | ParseExpr() method11Go | ParseDecl() method12Go | ParseFile() method13Go | ParseExprFrom() method14Go | ParseExprFrom() method15Go | ParseType() method16Go | ParseTypeFrom() method17Go | ParseDeclFrom() method18Go | ParseStmtFrom() method

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful