How to use Next method of parser Package

Best Gauge code snippet using parser.Next

expression.go

Source:expression.go Github

copy

Full Screen

1package parser2import (3 "regexp"4 "github.com/robertkrimen/otto/ast"5 "github.com/robertkrimen/otto/file"6 "github.com/robertkrimen/otto/token"7)8func (self *_parser) parseIdentifier() *ast.Identifier {9 literal := self.literal10 idx := self.idx11 if self.mode&StoreComments != 0 {12 self.comments.MarkComments(ast.LEADING)13 }14 self.next()15 exp := &ast.Identifier{16 Name: literal,17 Idx: idx,18 }19 if self.mode&StoreComments != 0 {20 self.comments.SetExpression(exp)21 }22 return exp23}24func (self *_parser) parsePrimaryExpression() ast.Expression {25 literal := self.literal26 idx := self.idx27 switch self.token {28 case token.IDENTIFIER:29 self.next()30 if len(literal) > 1 {31 tkn, strict := token.IsKeyword(literal)32 if tkn == token.KEYWORD {33 if !strict {34 self.error(idx, "Unexpected reserved word")35 }36 }37 }38 return &ast.Identifier{39 Name: literal,40 Idx: idx,41 }42 case token.NULL:43 self.next()44 return &ast.NullLiteral{45 Idx: idx,46 Literal: literal,47 }48 case token.BOOLEAN:49 self.next()50 value := false51 switch literal {52 case "true":53 value = true54 case "false":55 value = false56 default:57 self.error(idx, "Illegal boolean literal")58 }59 return &ast.BooleanLiteral{60 Idx: idx,61 Literal: literal,62 Value: value,63 }64 case token.STRING:65 self.next()66 value, err := parseStringLiteral(literal[1 : len(literal)-1])67 if err != nil {68 self.error(idx, err.Error())69 }70 return &ast.StringLiteral{71 Idx: idx,72 Literal: literal,73 Value: value,74 }75 case token.NUMBER:76 self.next()77 value, err := parseNumberLiteral(literal)78 if err != nil {79 self.error(idx, err.Error())80 value = 081 }82 return &ast.NumberLiteral{83 Idx: idx,84 Literal: literal,85 Value: value,86 }87 case token.SLASH, token.QUOTIENT_ASSIGN:88 return self.parseRegExpLiteral()89 case token.LEFT_BRACE:90 return self.parseObjectLiteral()91 case token.LEFT_BRACKET:92 return self.parseArrayLiteral()93 case token.LEFT_PARENTHESIS:94 self.expect(token.LEFT_PARENTHESIS)95 expression := self.parseExpression()96 if self.mode&StoreComments != 0 {97 self.comments.Unset()98 }99 self.expect(token.RIGHT_PARENTHESIS)100 return expression101 case token.THIS:102 self.next()103 return &ast.ThisExpression{104 Idx: idx,105 }106 case token.FUNCTION:107 return self.parseFunction(false)108 }109 self.errorUnexpectedToken(self.token)110 self.nextStatement()111 return &ast.BadExpression{From: idx, To: self.idx}112}113func (self *_parser) parseRegExpLiteral() *ast.RegExpLiteral {114 offset := self.chrOffset - 1 // Opening slash already gotten115 if self.token == token.QUOTIENT_ASSIGN {116 offset -= 1 // =117 }118 idx := self.idxOf(offset)119 pattern, err := self.scanString(offset)120 endOffset := self.chrOffset121 self.next()122 if err == nil {123 pattern = pattern[1 : len(pattern)-1]124 }125 flags := ""126 if self.token == token.IDENTIFIER { // gim127 flags = self.literal128 self.next()129 endOffset = self.chrOffset - 1130 }131 var value string132 // TODO 15.10133 {134 // Test during parsing that this is a valid regular expression135 // Sorry, (?=) and (?!) are invalid (for now)136 pattern, err := TransformRegExp(pattern)137 if err != nil {138 if pattern == "" || self.mode&IgnoreRegExpErrors == 0 {139 self.error(idx, "Invalid regular expression: %s", err.Error())140 }141 } else {142 _, err = regexp.Compile(pattern)143 if err != nil {144 // We should not get here, ParseRegExp should catch any errors145 self.error(idx, "Invalid regular expression: %s", err.Error()[22:]) // Skip redundant "parse regexp error"146 } else {147 value = pattern148 }149 }150 }151 literal := self.str[offset:endOffset]152 return &ast.RegExpLiteral{153 Idx: idx,154 Literal: literal,155 Pattern: pattern,156 Flags: flags,157 Value: value,158 }159}160func (self *_parser) parseVariableDeclaration(declarationList *[]*ast.VariableExpression) ast.Expression {161 if self.token != token.IDENTIFIER {162 idx := self.expect(token.IDENTIFIER)163 self.nextStatement()164 return &ast.BadExpression{From: idx, To: self.idx}165 }166 literal := self.literal167 idx := self.idx168 self.next()169 node := &ast.VariableExpression{170 Name: literal,171 Idx: idx,172 }173 if self.mode&StoreComments != 0 {174 self.comments.SetExpression(node)175 }176 if declarationList != nil {177 *declarationList = append(*declarationList, node)178 }179 if self.token == token.ASSIGN {180 if self.mode&StoreComments != 0 {181 self.comments.Unset()182 }183 self.next()184 node.Initializer = self.parseAssignmentExpression()185 }186 return node187}188func (self *_parser) parseVariableDeclarationList(var_ file.Idx) []ast.Expression {189 var declarationList []*ast.VariableExpression // Avoid bad expressions190 var list []ast.Expression191 for {192 if self.mode&StoreComments != 0 {193 self.comments.MarkComments(ast.LEADING)194 }195 decl := self.parseVariableDeclaration(&declarationList)196 list = append(list, decl)197 if self.token != token.COMMA {198 break199 }200 if self.mode&StoreComments != 0 {201 self.comments.Unset()202 }203 self.next()204 }205 self.scope.declare(&ast.VariableDeclaration{206 Var: var_,207 List: declarationList,208 })209 return list210}211func (self *_parser) parseObjectPropertyKey() (string, string) {212 idx, tkn, literal := self.idx, self.token, self.literal213 value := ""214 if self.mode&StoreComments != 0 {215 self.comments.MarkComments(ast.KEY)216 }217 self.next()218 switch tkn {219 case token.IDENTIFIER:220 value = literal221 case token.NUMBER:222 var err error223 _, err = parseNumberLiteral(literal)224 if err != nil {225 self.error(idx, err.Error())226 } else {227 value = literal228 }229 case token.STRING:230 var err error231 value, err = parseStringLiteral(literal[1 : len(literal)-1])232 if err != nil {233 self.error(idx, err.Error())234 }235 default:236 // null, false, class, etc.237 if matchIdentifier.MatchString(literal) {238 value = literal239 }240 }241 return literal, value242}243func (self *_parser) parseObjectProperty() ast.Property {244 literal, value := self.parseObjectPropertyKey()245 if literal == "get" && self.token != token.COLON {246 idx := self.idx247 _, value := self.parseObjectPropertyKey()248 parameterList := self.parseFunctionParameterList()249 node := &ast.FunctionLiteral{250 Function: idx,251 ParameterList: parameterList,252 }253 self.parseFunctionBlock(node)254 return ast.Property{255 Key: value,256 Kind: "get",257 Value: node,258 }259 } else if literal == "set" && self.token != token.COLON {260 idx := self.idx261 _, value := self.parseObjectPropertyKey()262 parameterList := self.parseFunctionParameterList()263 node := &ast.FunctionLiteral{264 Function: idx,265 ParameterList: parameterList,266 }267 self.parseFunctionBlock(node)268 return ast.Property{269 Key: value,270 Kind: "set",271 Value: node,272 }273 }274 if self.mode&StoreComments != 0 {275 self.comments.MarkComments(ast.COLON)276 }277 self.expect(token.COLON)278 exp := ast.Property{279 Key: value,280 Kind: "value",281 Value: self.parseAssignmentExpression(),282 }283 if self.mode&StoreComments != 0 {284 self.comments.SetExpression(exp.Value)285 }286 return exp287}288func (self *_parser) parseObjectLiteral() ast.Expression {289 var value []ast.Property290 idx0 := self.expect(token.LEFT_BRACE)291 for self.token != token.RIGHT_BRACE && self.token != token.EOF {292 value = append(value, self.parseObjectProperty())293 if self.token == token.COMMA {294 if self.mode&StoreComments != 0 {295 self.comments.Unset()296 }297 self.next()298 continue299 }300 }301 if self.mode&StoreComments != 0 {302 self.comments.MarkComments(ast.FINAL)303 }304 idx1 := self.expect(token.RIGHT_BRACE)305 return &ast.ObjectLiteral{306 LeftBrace: idx0,307 RightBrace: idx1,308 Value: value,309 }310}311func (self *_parser) parseArrayLiteral() ast.Expression {312 idx0 := self.expect(token.LEFT_BRACKET)313 var value []ast.Expression314 for self.token != token.RIGHT_BRACKET && self.token != token.EOF {315 if self.token == token.COMMA {316 // This kind of comment requires a special empty expression node.317 empty := &ast.EmptyExpression{self.idx, self.idx}318 if self.mode&StoreComments != 0 {319 self.comments.SetExpression(empty)320 self.comments.Unset()321 }322 value = append(value, empty)323 self.next()324 continue325 }326 exp := self.parseAssignmentExpression()327 value = append(value, exp)328 if self.token != token.RIGHT_BRACKET {329 if self.mode&StoreComments != 0 {330 self.comments.Unset()331 }332 self.expect(token.COMMA)333 }334 }335 if self.mode&StoreComments != 0 {336 self.comments.MarkComments(ast.FINAL)337 }338 idx1 := self.expect(token.RIGHT_BRACKET)339 return &ast.ArrayLiteral{340 LeftBracket: idx0,341 RightBracket: idx1,342 Value: value,343 }344}345func (self *_parser) parseArgumentList() (argumentList []ast.Expression, idx0, idx1 file.Idx) {346 if self.mode&StoreComments != 0 {347 self.comments.Unset()348 }349 idx0 = self.expect(token.LEFT_PARENTHESIS)350 if self.token != token.RIGHT_PARENTHESIS {351 for {352 exp := self.parseAssignmentExpression()353 if self.mode&StoreComments != 0 {354 self.comments.SetExpression(exp)355 }356 argumentList = append(argumentList, exp)357 if self.token != token.COMMA {358 break359 }360 if self.mode&StoreComments != 0 {361 self.comments.Unset()362 }363 self.next()364 }365 }366 if self.mode&StoreComments != 0 {367 self.comments.Unset()368 }369 idx1 = self.expect(token.RIGHT_PARENTHESIS)370 return371}372func (self *_parser) parseCallExpression(left ast.Expression) ast.Expression {373 argumentList, idx0, idx1 := self.parseArgumentList()374 exp := &ast.CallExpression{375 Callee: left,376 LeftParenthesis: idx0,377 ArgumentList: argumentList,378 RightParenthesis: idx1,379 }380 if self.mode&StoreComments != 0 {381 self.comments.SetExpression(exp)382 }383 return exp384}385func (self *_parser) parseDotMember(left ast.Expression) ast.Expression {386 period := self.expect(token.PERIOD)387 literal := self.literal388 idx := self.idx389 if !matchIdentifier.MatchString(literal) {390 self.expect(token.IDENTIFIER)391 self.nextStatement()392 return &ast.BadExpression{From: period, To: self.idx}393 }394 self.next()395 return &ast.DotExpression{396 Left: left,397 Identifier: &ast.Identifier{398 Idx: idx,399 Name: literal,400 },401 }402}403func (self *_parser) parseBracketMember(left ast.Expression) ast.Expression {404 idx0 := self.expect(token.LEFT_BRACKET)405 member := self.parseExpression()406 idx1 := self.expect(token.RIGHT_BRACKET)407 return &ast.BracketExpression{408 LeftBracket: idx0,409 Left: left,410 Member: member,411 RightBracket: idx1,412 }413}414func (self *_parser) parseNewExpression() ast.Expression {415 idx := self.expect(token.NEW)416 callee := self.parseLeftHandSideExpression()417 node := &ast.NewExpression{418 New: idx,419 Callee: callee,420 }421 if self.token == token.LEFT_PARENTHESIS {422 argumentList, idx0, idx1 := self.parseArgumentList()423 node.ArgumentList = argumentList424 node.LeftParenthesis = idx0425 node.RightParenthesis = idx1426 }427 if self.mode&StoreComments != 0 {428 self.comments.SetExpression(node)429 }430 return node431}432func (self *_parser) parseLeftHandSideExpression() ast.Expression {433 var left ast.Expression434 if self.token == token.NEW {435 left = self.parseNewExpression()436 } else {437 if self.mode&StoreComments != 0 {438 self.comments.MarkComments(ast.LEADING)439 self.comments.MarkPrimary()440 }441 left = self.parsePrimaryExpression()442 }443 if self.mode&StoreComments != 0 {444 self.comments.SetExpression(left)445 }446 for {447 if self.token == token.PERIOD {448 left = self.parseDotMember(left)449 } else if self.token == token.LEFT_BRACKET {450 left = self.parseBracketMember(left)451 } else {452 break453 }454 }455 return left456}457func (self *_parser) parseLeftHandSideExpressionAllowCall() ast.Expression {458 allowIn := self.scope.allowIn459 self.scope.allowIn = true460 defer func() {461 self.scope.allowIn = allowIn462 }()463 var left ast.Expression464 if self.token == token.NEW {465 var newComments []*ast.Comment466 if self.mode&StoreComments != 0 {467 newComments = self.comments.FetchAll()468 self.comments.MarkComments(ast.LEADING)469 self.comments.MarkPrimary()470 }471 left = self.parseNewExpression()472 if self.mode&StoreComments != 0 {473 self.comments.CommentMap.AddComments(left, newComments, ast.LEADING)474 }475 } else {476 if self.mode&StoreComments != 0 {477 self.comments.MarkComments(ast.LEADING)478 self.comments.MarkPrimary()479 }480 left = self.parsePrimaryExpression()481 }482 if self.mode&StoreComments != 0 {483 self.comments.SetExpression(left)484 }485 for {486 if self.token == token.PERIOD {487 left = self.parseDotMember(left)488 } else if self.token == token.LEFT_BRACKET {489 left = self.parseBracketMember(left)490 } else if self.token == token.LEFT_PARENTHESIS {491 left = self.parseCallExpression(left)492 } else {493 break494 }495 }496 return left497}498func (self *_parser) parsePostfixExpression() ast.Expression {499 operand := self.parseLeftHandSideExpressionAllowCall()500 switch self.token {501 case token.INCREMENT, token.DECREMENT:502 // Make sure there is no line terminator here503 if self.implicitSemicolon {504 break505 }506 tkn := self.token507 idx := self.idx508 if self.mode&StoreComments != 0 {509 self.comments.Unset()510 }511 self.next()512 switch operand.(type) {513 case *ast.Identifier, *ast.DotExpression, *ast.BracketExpression:514 default:515 self.error(idx, "Invalid left-hand side in assignment")516 self.nextStatement()517 return &ast.BadExpression{From: idx, To: self.idx}518 }519 exp := &ast.UnaryExpression{520 Operator: tkn,521 Idx: idx,522 Operand: operand,523 Postfix: true,524 }525 if self.mode&StoreComments != 0 {526 self.comments.SetExpression(exp)527 }528 return exp529 }530 return operand531}532func (self *_parser) parseUnaryExpression() ast.Expression {533 switch self.token {534 case token.PLUS, token.MINUS, token.NOT, token.BITWISE_NOT:535 fallthrough536 case token.DELETE, token.VOID, token.TYPEOF:537 tkn := self.token538 idx := self.idx539 if self.mode&StoreComments != 0 {540 self.comments.Unset()541 }542 self.next()543 return &ast.UnaryExpression{544 Operator: tkn,545 Idx: idx,546 Operand: self.parseUnaryExpression(),547 }548 case token.INCREMENT, token.DECREMENT:549 tkn := self.token550 idx := self.idx551 if self.mode&StoreComments != 0 {552 self.comments.Unset()553 }554 self.next()555 operand := self.parseUnaryExpression()556 switch operand.(type) {557 case *ast.Identifier, *ast.DotExpression, *ast.BracketExpression:558 default:559 self.error(idx, "Invalid left-hand side in assignment")560 self.nextStatement()561 return &ast.BadExpression{From: idx, To: self.idx}562 }563 return &ast.UnaryExpression{564 Operator: tkn,565 Idx: idx,566 Operand: operand,567 }568 }569 return self.parsePostfixExpression()570}571func (self *_parser) parseMultiplicativeExpression() ast.Expression {572 next := self.parseUnaryExpression573 left := next()574 for self.token == token.MULTIPLY || self.token == token.SLASH ||575 self.token == token.REMAINDER {576 tkn := self.token577 if self.mode&StoreComments != 0 {578 self.comments.Unset()579 }580 self.next()581 left = &ast.BinaryExpression{582 Operator: tkn,583 Left: left,584 Right: next(),585 }586 }587 return left588}589func (self *_parser) parseAdditiveExpression() ast.Expression {590 next := self.parseMultiplicativeExpression591 left := next()592 for self.token == token.PLUS || self.token == token.MINUS {593 tkn := self.token594 if self.mode&StoreComments != 0 {595 self.comments.Unset()596 }597 self.next()598 left = &ast.BinaryExpression{599 Operator: tkn,600 Left: left,601 Right: next(),602 }603 }604 return left605}606func (self *_parser) parseShiftExpression() ast.Expression {607 next := self.parseAdditiveExpression608 left := next()609 for self.token == token.SHIFT_LEFT || self.token == token.SHIFT_RIGHT ||610 self.token == token.UNSIGNED_SHIFT_RIGHT {611 tkn := self.token612 if self.mode&StoreComments != 0 {613 self.comments.Unset()614 }615 self.next()616 left = &ast.BinaryExpression{617 Operator: tkn,618 Left: left,619 Right: next(),620 }621 }622 return left623}624func (self *_parser) parseRelationalExpression() ast.Expression {625 next := self.parseShiftExpression626 left := next()627 allowIn := self.scope.allowIn628 self.scope.allowIn = true629 defer func() {630 self.scope.allowIn = allowIn631 }()632 switch self.token {633 case token.LESS, token.LESS_OR_EQUAL, token.GREATER, token.GREATER_OR_EQUAL:634 tkn := self.token635 if self.mode&StoreComments != 0 {636 self.comments.Unset()637 }638 self.next()639 exp := &ast.BinaryExpression{640 Operator: tkn,641 Left: left,642 Right: self.parseRelationalExpression(),643 Comparison: true,644 }645 return exp646 case token.INSTANCEOF:647 tkn := self.token648 if self.mode&StoreComments != 0 {649 self.comments.Unset()650 }651 self.next()652 exp := &ast.BinaryExpression{653 Operator: tkn,654 Left: left,655 Right: self.parseRelationalExpression(),656 }657 return exp658 case token.IN:659 if !allowIn {660 return left661 }662 tkn := self.token663 if self.mode&StoreComments != 0 {664 self.comments.Unset()665 }666 self.next()667 exp := &ast.BinaryExpression{668 Operator: tkn,669 Left: left,670 Right: self.parseRelationalExpression(),671 }672 return exp673 }674 return left675}676func (self *_parser) parseEqualityExpression() ast.Expression {677 next := self.parseRelationalExpression678 left := next()679 for self.token == token.EQUAL || self.token == token.NOT_EQUAL ||680 self.token == token.STRICT_EQUAL || self.token == token.STRICT_NOT_EQUAL {681 tkn := self.token682 if self.mode&StoreComments != 0 {683 self.comments.Unset()684 }685 self.next()686 left = &ast.BinaryExpression{687 Operator: tkn,688 Left: left,689 Right: next(),690 Comparison: true,691 }692 }693 return left694}695func (self *_parser) parseBitwiseAndExpression() ast.Expression {696 next := self.parseEqualityExpression697 left := next()698 for self.token == token.AND {699 if self.mode&StoreComments != 0 {700 self.comments.Unset()701 }702 tkn := self.token703 self.next()704 left = &ast.BinaryExpression{705 Operator: tkn,706 Left: left,707 Right: next(),708 }709 }710 return left711}712func (self *_parser) parseBitwiseExclusiveOrExpression() ast.Expression {713 next := self.parseBitwiseAndExpression714 left := next()715 for self.token == token.EXCLUSIVE_OR {716 if self.mode&StoreComments != 0 {717 self.comments.Unset()718 }719 tkn := self.token720 self.next()721 left = &ast.BinaryExpression{722 Operator: tkn,723 Left: left,724 Right: next(),725 }726 }727 return left728}729func (self *_parser) parseBitwiseOrExpression() ast.Expression {730 next := self.parseBitwiseExclusiveOrExpression731 left := next()732 for self.token == token.OR {733 if self.mode&StoreComments != 0 {734 self.comments.Unset()735 }736 tkn := self.token737 self.next()738 left = &ast.BinaryExpression{739 Operator: tkn,740 Left: left,741 Right: next(),742 }743 }744 return left745}746func (self *_parser) parseLogicalAndExpression() ast.Expression {747 next := self.parseBitwiseOrExpression748 left := next()749 for self.token == token.LOGICAL_AND {750 if self.mode&StoreComments != 0 {751 self.comments.Unset()752 }753 tkn := self.token754 self.next()755 left = &ast.BinaryExpression{756 Operator: tkn,757 Left: left,758 Right: next(),759 }760 }761 return left762}763func (self *_parser) parseLogicalOrExpression() ast.Expression {764 next := self.parseLogicalAndExpression765 left := next()766 for self.token == token.LOGICAL_OR {767 if self.mode&StoreComments != 0 {768 self.comments.Unset()769 }770 tkn := self.token771 self.next()772 left = &ast.BinaryExpression{773 Operator: tkn,774 Left: left,775 Right: next(),776 }777 }778 return left779}780func (self *_parser) parseConditionlExpression() ast.Expression {781 left := self.parseLogicalOrExpression()782 if self.token == token.QUESTION_MARK {783 if self.mode&StoreComments != 0 {784 self.comments.Unset()785 }786 self.next()787 consequent := self.parseAssignmentExpression()788 if self.mode&StoreComments != 0 {789 self.comments.Unset()790 }791 self.expect(token.COLON)792 exp := &ast.ConditionalExpression{793 Test: left,794 Consequent: consequent,795 Alternate: self.parseAssignmentExpression(),796 }797 return exp798 }799 return left800}801func (self *_parser) parseAssignmentExpression() ast.Expression {802 left := self.parseConditionlExpression()803 var operator token.Token804 switch self.token {805 case token.ASSIGN:806 operator = self.token807 case token.ADD_ASSIGN:808 operator = token.PLUS809 case token.SUBTRACT_ASSIGN:810 operator = token.MINUS811 case token.MULTIPLY_ASSIGN:812 operator = token.MULTIPLY813 case token.QUOTIENT_ASSIGN:814 operator = token.SLASH815 case token.REMAINDER_ASSIGN:816 operator = token.REMAINDER817 case token.AND_ASSIGN:818 operator = token.AND819 case token.AND_NOT_ASSIGN:820 operator = token.AND_NOT821 case token.OR_ASSIGN:822 operator = token.OR823 case token.EXCLUSIVE_OR_ASSIGN:824 operator = token.EXCLUSIVE_OR825 case token.SHIFT_LEFT_ASSIGN:826 operator = token.SHIFT_LEFT827 case token.SHIFT_RIGHT_ASSIGN:828 operator = token.SHIFT_RIGHT829 case token.UNSIGNED_SHIFT_RIGHT_ASSIGN:830 operator = token.UNSIGNED_SHIFT_RIGHT831 }832 if operator != 0 {833 idx := self.idx834 if self.mode&StoreComments != 0 {835 self.comments.Unset()836 }837 self.next()838 switch left.(type) {839 case *ast.Identifier, *ast.DotExpression, *ast.BracketExpression:840 default:841 self.error(left.Idx0(), "Invalid left-hand side in assignment")842 self.nextStatement()843 return &ast.BadExpression{From: idx, To: self.idx}844 }845 exp := &ast.AssignExpression{846 Left: left,847 Operator: operator,848 Right: self.parseAssignmentExpression(),849 }850 if self.mode&StoreComments != 0 {851 self.comments.SetExpression(exp)852 }853 return exp854 }855 return left856}857func (self *_parser) parseExpression() ast.Expression {858 next := self.parseAssignmentExpression859 left := next()860 if self.token == token.COMMA {861 sequence := []ast.Expression{left}862 for {863 if self.token != token.COMMA {864 break865 }866 self.next()867 sequence = append(sequence, next())868 }869 return &ast.SequenceExpression{870 Sequence: sequence,871 }872 }873 return left874}...

Full Screen

Full Screen

parser.go

Source:parser.go Github

copy

Full Screen

...49 p.errors = append(p.errors, msg)50}51func (p *Parser) nextToken() {52 p.curToken = p.peekToken53 p.peekToken = p.l.NextToken()54}55func (p *Parser) ParseProgram() *ast.Program {56 program := &ast.Program{}57 program.Statements = []ast.Statement{}58 for p.curToken.Type != token.EOF {59 stmt := p.parseStatement()60 if stmt != nil {61 program.Statements = append(program.Statements, stmt)62 }63 p.nextToken()64 }65 return program66}67func (p *Parser) parseStatement() ast.Statement {...

Full Screen

Full Screen

Next

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 doc, err := html.Parse(os.Stdin)4 if err != nil {5 fmt.Fprintf(os.Stderr, "findlinks1: %v6 os.Exit(1)7 }8 for _, link := range visit(nil, doc) {9 fmt.Println(link)10 }11}12func visit(links []string, n *html.Node) []string {13 if n.Type == html.ElementNode && n.Data == "a" {14 for _, a := range n.Attr {15 if a.Key == "href" {16 links = append(links, a.Val)17 }18 }19 }20 for c := n.FirstChild; c != nil; c = c.NextSibling {21 links = visit(links, c)22 }23}24import (25func main() {26 doc, err := html.Parse(os.Stdin)27 if err != nil {28 fmt.Fprintf(os.Stderr, "findlinks2: %v29 os.Exit(1)30 }31 for _, link := range visit(nil, doc) {32 fmt.Println(link)33 }34}35func visit(links []string, n *html.Node) []string {36 if n.Type == html.ElementNode && n.Data == "a" {37 for _, a := range n.Attr {38 if a.Key == "href" {39 links = append(links, a.Val)40 }41 }42 }43 links = traverse(links, n.FirstChild)44}45func traverse(links []string, n *html.Node) []string {46 if n == nil {47 }48 links = visit(links, n)49 links = traverse(links, n.NextSibling)50}51import (52func main() {53 doc, err := html.Parse(os.Stdin)54 if err != nil {55 fmt.Fprintf(os.Stderr, "findlinks3: %v56 os.Exit(1)57 }58 for _, link := range visit(nil, doc

Full Screen

Full Screen

Next

Using AI Code Generation

copy

Full Screen

1import (2type Person struct {3}4func main() {5 v := &Person{Name: "none", Age: 0}6 dec := xml.NewDecoder(os.Stdin)7 for {8 t, _ := dec.Token()9 if t == nil {10 }11 switch se := t.(type) {12 if se.Name.Local == "name" {13 if err := dec.DecodeElement(&v.Name, &se); err != nil {14 fmt.Println(err)15 }16 }17 if se.Name.Local == "age" {18 if err := dec.DecodeElement(&v.Age, &se); err != nil {19 fmt.Println(err)20 }21 }22 if se.Name.Local == "email" {23 if err := dec.DecodeElement(&email, &se); err != nil {24 fmt.Println(err)25 }26 v.Emails = append(v.Emails, email)27 }28 }29 }30 fmt.Println(v)31}32&{person none 30 [

Full Screen

Full Screen

Next

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 fmt.Println(err)5 }6 doc.Find(".mw-headline").Each(func(i int, s *goquery.Selection) {7 fmt.Println(s.Text())8 })9}

Full Screen

Full Screen

Next

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 fmt.Println(err)5 }6 defer resp.Body.Close()7 z := html.NewTokenizer(resp.Body)8 for {9 tt := z.Next()10 switch tt {11 t := z.Token()12 fmt.Printf("%v13 t := z.Token()14 fmt.Printf("%v15 }16 }17}

Full Screen

Full Screen

Next

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 import "fmt"4 func main() {5 fmt.Println("Hello, 世界")6 }`7 f, err := parser.ParseFile(fset, "hello.go", src, parser.ImportsOnly)8 if err != nil {9 fmt.Println(err)10 }11 for _, s := range f.Imports {12 fmt.Println(s.Path.Value)13 }14}15import (16func main() {17 import "fmt"18 func main() {19 fmt.Println("Hello, 世界")20 }`21 f, err := parser.ParseExpr(src)22 if err != nil {23 fmt.Println(err)24 }25 fmt.Println(f)26}27import (28func main() {29 import "fmt"30 func main() {31 fmt.Println("Hello, 世界")32 }`33 f, err := parser.ParseExprFrom(fset, "hello.go", src, 0)34 if err != nil {

Full Screen

Full Screen

Next

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 p := filepath.NewParser()4 p.Parse("C:\\Users\\User\\Desktop\\Golang\\1.go")5 for p.Next() {6 fmt.Println(p.Path())7 }8}9import (10func main() {11 fmt.Println(filepath.Split("C:\\Users\\User\\Desktop\\Golang\\1.go"))12 fmt.Println(filepath.Split("C:\\Users\\User\\Desktop\\Golang"))13}14import (15func main() {16 fmt.Println(filepath.SplitList("C:\\Users\\User\\Desktop\\Golang\\1.go"))17 fmt.Println(filepath.SplitList("C:\\Users\\User\\Desktop\\Golang"))18}19import (20func main() {21 fmt.Println(filepath.Split("C:\\Users\\User\\Desktop\\Golang\\1.go"))22 fmt.Println(filepath.Split("C:\\Users\\User\\Desktop\\Golang"))23}24import (25func main() {26 fmt.Println(filepath.ToSlash("C:\\Users\\User\\Desktop\\Golang\\1.go"))27 fmt.Println(filepath.ToSlash("C:\\Users\\User\\Desktop\\Golang"))28}

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.

Run Gauge automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful