Best Syzkaller code snippet using parser.Lookahead
parser.go
Source:parser.go
1package language2import (3 "fmt"4 "strconv"5 "strings"6)7type Parser struct {8 tokens chan Token9 lookahead *Token10 prevEnd *Position11 source string12 ast interface{}13 noSource bool14}15type ParseParams struct {16 Source string17 NoSource bool18}19func (parser *Parser) Parse(params *ParseParams) (*Document, error) {20 parser.source = params.Source21 parser.noSource = params.NoSource22 parser.tokens = Lex(LexText, parser.source)23 token := <-parser.tokens24 if token.Type == ILLEGAL {25 return nil, &GraphQLError{26 Message: token.Val,27 Source: parser.source,28 Start: token.Start,29 End: token.End,30 }31 }32 parser.lookahead = &token33 return parser.document()34}35func (parser *Parser) match(symbol TokenType) error {36 if parser.lookahead.Type == symbol {37 parser.prevEnd = parser.lookahead.End38 token := <-parser.tokens39 if token.Type == ILLEGAL {40 return &GraphQLError{41 Message: token.Val,42 Source: parser.source,43 Start: token.Start,44 End: token.End,45 }46 }47 parser.lookahead = &token48 return nil49 } else {50 return &GraphQLError{51 Message: fmt.Sprintf("GraphQL Syntax Error (%d:%d) Expected %s, found %s", parser.lookahead.Start.Line, parser.lookahead.Start.Column, symbol, parser.lookahead.String()),52 Source: parser.source,53 Start: parser.lookahead.Start,54 End: parser.lookahead.End,55 }56 }57}58func (parser *Parser) matchName(value string) error {59 if parser.lookahead.Type == NAME && parser.lookahead.Val == value {60 parser.prevEnd = parser.lookahead.End61 token := <-parser.tokens62 parser.lookahead = &token63 return nil64 } else {65 return &GraphQLError{66 Message: fmt.Sprintf("GraphQL Syntax Error (%d:%d) Expected \"%s\", found %s", parser.lookahead.Start.Line, parser.lookahead.Start.Column, value, parser.lookahead.String()),67 Source: parser.source,68 Start: parser.lookahead.Start,69 End: parser.lookahead.End,70 }71 }72}73func (parser *Parser) value() (ASTNode, error) {74 return parser.valueLiteral(false)75}76func (parser *Parser) loc(start *Position) *LOC {77 if parser.noSource {78 return &LOC{79 Start: start,80 End: parser.prevEnd,81 }82 } else {83 return &LOC{84 Start: start,85 End: parser.prevEnd,86 Source: parser.source,87 }88 }89}90func (parser *Parser) name() (*Name, error) {91 token := parser.lookahead92 err := parser.match(NAME)93 if err != nil {94 return nil, err95 }96 return &Name{97 Value: token.Val,98 LOC: parser.loc(token.Start),99 }, nil100}101func (parser *Parser) description() (string, error) {102 text := ""103 isBody := false104 token := parser.lookahead105 for token.Type == DESCRIPTION {106 text += strings.Trim(token.Val[2:], " ")107 if isBody {108 text += " "109 } else {110 isBody = true111 }112 err := parser.match(DESCRIPTION)113 if err != nil {114 return text, err115 }116 token = parser.lookahead117 }118 return text, nil119}120/**121 * Document : Definition+122 */123func (parser *Parser) document() (*Document, error) {124 start := parser.lookahead.Start125 definitions := []ASTNode{}126 fragmentIndex := map[string]*FragmentDefinition{}127 objectTypeIndex := map[string]*ObjectTypeDefinition{}128 interfaceTypeIndex := map[string]*InterfaceTypeDefinition{}129 unionTypeIndex := map[string]*UnionTypeDefinition{}130 inputObjectTypeIndex := map[string]*InputObjectTypeDefinition{}131 scalarTypeIndex := map[string]*ScalarTypeDefinition{}132 enumTypeIndex := map[string]*EnumTypeDefinition{}133 typeIndex := map[string]ASTNode{}134 typeExtensionIndex := map[string]*TypeExtensionDefinition{}135 possibleTypesIndex := map[string][]*ObjectTypeDefinition{}136 for {137 definition, err := parser.definition()138 if err != nil {139 return nil, err140 }141 switch item := definition.(type) {142 case *FragmentDefinition:143 fragmentIndex[item.Name.Value] = item144 typeIndex[item.Name.Value] = item145 case *ObjectTypeDefinition:146 objectTypeIndex[item.Name.Value] = item147 typeIndex[item.Name.Value] = item148 if len(item.Interfaces) > 0 {149 for _, implementedInterface := range item.Interfaces {150 interfaceName := implementedInterface.Name.Value151 if possibleTypesIndex[interfaceName] == nil {152 possibleTypesIndex[interfaceName] = []*ObjectTypeDefinition{}153 }154 possibleTypesIndex[interfaceName] = append(possibleTypesIndex[interfaceName], item)155 }156 }157 case *TypeExtensionDefinition:158 typeExtensionIndex[item.Definition.Name.Value] = item159 case *InterfaceTypeDefinition:160 interfaceTypeIndex[item.Name.Value] = item161 typeIndex[item.Name.Value] = item162 case *UnionTypeDefinition:163 unionTypeIndex[item.Name.Value] = item164 typeIndex[item.Name.Value] = item165 case *InputObjectTypeDefinition:166 inputObjectTypeIndex[item.Name.Value] = item167 typeIndex[item.Name.Value] = item168 case *EnumTypeDefinition:169 enumTypeIndex[item.Name.Value] = item170 typeIndex[item.Name.Value] = item171 case *ScalarTypeDefinition:172 scalarTypeIndex[item.Name.Value] = item173 typeIndex[item.Name.Value] = item174 }175 definitions = append(definitions, definition)176 if parser.lookahead.Type == EOF {177 break178 }179 }180 // Find possible types for unions181 for _, unionType := range unionTypeIndex {182 unionName := unionType.Name.Value183 if possibleTypesIndex[unionName] == nil {184 possibleTypesIndex[unionName] = []*ObjectTypeDefinition{}185 }186 for _, possibleType := range unionType.Types {187 possibleTypesIndex[unionName] = append(possibleTypesIndex[unionName], objectTypeIndex[possibleType.Name.Value])188 }189 }190 if len(fragmentIndex) == 0 {191 fragmentIndex = nil192 }193 if len(objectTypeIndex) == 0 {194 objectTypeIndex = nil195 }196 if len(typeExtensionIndex) == 0 {197 typeExtensionIndex = nil198 }199 if len(interfaceTypeIndex) == 0 {200 interfaceTypeIndex = nil201 }202 if len(unionTypeIndex) == 0 {203 unionTypeIndex = nil204 }205 if len(inputObjectTypeIndex) == 0 {206 inputObjectTypeIndex = nil207 }208 if len(scalarTypeIndex) == 0 {209 scalarTypeIndex = nil210 }211 if len(enumTypeIndex) == 0 {212 enumTypeIndex = nil213 }214 if len(possibleTypesIndex) == 0 {215 possibleTypesIndex = nil216 }217 if len(typeIndex) == 0 {218 typeIndex = nil219 }220 return &Document{221 Definitions: definitions,222 FragmentIndex: fragmentIndex,223 ObjectTypeIndex: objectTypeIndex,224 TypeExtensionIndex: typeExtensionIndex,225 InterfaceTypeIndex: interfaceTypeIndex,226 UnionTypeIndex: unionTypeIndex,227 InputObjectTypeIndex: inputObjectTypeIndex,228 ScalarTypeIndex: scalarTypeIndex,229 EnumTypeIndex: enumTypeIndex,230 PossibleTypesIndex: possibleTypesIndex,231 TypeIndex: typeIndex,232 LOC: parser.loc(start),233 }, nil234}235/**236 * Definition :237 * - OperationDefinition238 * - FragmentDefinition239 * - TypeDefinition240 * - TypeExtensionDefinition241 */242func (parser *Parser) definition() (ASTNode, error) {243 description, err := parser.description()244 if err != nil {245 return nil, err246 }247 switch parser.lookahead.Type {248 case NAME:249 switch parser.lookahead.Val {250 case "fragment":251 return parser.fragmentDefinition()252 case "mutation", "query", "subscription":253 return parser.operationDefinition()254 case "type", "interface", "union", "scalar", "enum", "input":255 return parser.typeDefinition(description)256 case "extend":257 return parser.typeExtensionDefinition(description)258 default:259 return nil, &GraphQLError{260 Message: fmt.Sprintf("GraphQL Syntax Error (%d:%d) Unexpected %s", parser.lookahead.Start.Line, parser.lookahead.Start.Column, parser.lookahead.String()),261 Source: parser.source,262 Start: parser.lookahead.Start,263 End: parser.lookahead.End,264 }265 }266 case LBRACE:267 return parser.operationDefinition()268 default:269 return nil, &GraphQLError{270 Message: fmt.Sprintf("GraphQL Syntax Error (%d:%d) Unexpected %s", parser.lookahead.Start.Line, parser.lookahead.Start.Column, parser.lookahead.String()),271 Source: parser.source,272 Start: parser.lookahead.Start,273 End: parser.lookahead.End,274 }275 }276}277/**278 * OperationDefinition :279 * - SelectionSet280 * - OperationType Name? VariableDefinitions? Directives? SelectionSet281 *282 * OperationType : one of query mutation subscription283 */284func (parser *Parser) operationDefinition() (ASTNode, error) {285 start := parser.lookahead.Start286 switch parser.lookahead.Type {287 case LBRACE:288 selectionSet, err := parser.selectionSet()289 if err != nil {290 return nil, err291 }292 return &OperationDefinition{293 Operation: "query",294 SelectionSet: selectionSet,295 LOC: parser.loc(start),296 }, nil297 case NAME:298 node := &OperationDefinition{}299 node.Operation = parser.lookahead.Val300 err := parser.matchName(parser.lookahead.Val)301 if err != nil {302 return nil, err303 }304 if parser.lookahead.Type == NAME {305 node.Name, err = parser.name()306 if err != nil {307 return nil, err308 }309 }310 if parser.lookahead.Type == LPAREN {311 node.VariableDefinitions, node.VariableDefinitionIndex, err = parser.variableDefinitions()312 if err != nil {313 return nil, err314 }315 }316 if parser.lookahead.Type == AT {317 node.Directives, node.DirectiveIndex, err = parser.directives()318 if err != nil {319 return nil, err320 }321 }322 node.SelectionSet, err = parser.selectionSet()323 if err != nil {324 return nil, err325 }326 node.LOC = parser.loc(start)327 return node, nil328 default:329 return nil, &GraphQLError{330 Message: fmt.Sprintf("GraphQL Syntax Error (%d:%d) Unexpected %s", parser.lookahead.Start.Line, parser.lookahead.Start.Column, parser.lookahead.String()),331 Source: parser.source,332 Start: parser.lookahead.Start,333 End: parser.lookahead.End,334 }335 }336}337/**338 * VariableDefinitions : ( VariableDefinition+ )339 */340func (parser *Parser) variableDefinitions() ([]*VariableDefinition, map[string]*VariableDefinition, error) {341 variableDefinitions := []*VariableDefinition{}342 variableDefinitionIndex := map[string]*VariableDefinition{}343 err := parser.match(LPAREN)344 if err != nil {345 return nil, nil, err346 }347 for {348 variableDefinition, err := parser.variableDefinition()349 if err != nil {350 return nil, nil, err351 }352 variableDefinitions = append(variableDefinitions, variableDefinition)353 variableDefinitionIndex[variableDefinition.Variable.Name.Value] = variableDefinition354 if parser.lookahead.Type == RPAREN {355 break356 }357 }358 if len(variableDefinitions) == 0 {359 variableDefinitionIndex = nil360 }361 err = parser.match(RPAREN)362 if err != nil {363 return nil, nil, err364 }365 return variableDefinitions, variableDefinitionIndex, nil366}367/**368 * VariableDefinition : Variable : Type DefaultValue?369 */370func (parser *Parser) variableDefinition() (*VariableDefinition, error) {371 var err error372 start := parser.lookahead.Start373 node := &VariableDefinition{}374 node.Variable, err = parser.variable()375 if err != nil {376 return nil, err377 }378 err = parser.match(COLON)379 if err != nil {380 return nil, err381 }382 node.Type, err = parser.type_()383 if err != nil {384 return nil, err385 }386 if parser.lookahead.Type == EQ {387 err = parser.match(EQ)388 if err != nil {389 return nil, err390 }391 node.DefaultValue, err = parser.valueLiteral(true)392 if err != nil {393 return nil, err394 }395 }396 node.LOC = parser.loc(start)397 return node, nil398}399/**400 * Variable : $ Name401 */402func (parser *Parser) variable() (*Variable, error) {403 var err error404 start := parser.lookahead.Start405 node := &Variable{}406 err = parser.match(DOLLAR)407 if err != nil {408 return nil, err409 }410 node.Name, err = parser.name()411 if err != nil {412 return nil, err413 }414 node.LOC = parser.loc(start)415 return node, nil416}417/**418 * SelectionSet : { Selection+ }419 */420func (parser *Parser) selectionSet() (*SelectionSet, error) {421 start := parser.lookahead.Start422 node := &SelectionSet{}423 err := parser.match(LBRACE)424 if err != nil {425 return nil, err426 }427 for {428 selection, err := parser.selection()429 if err != nil {430 return nil, err431 }432 node.Selections = append(node.Selections, selection)433 if parser.lookahead.Type == RBRACE {434 break435 }436 }437 err = parser.match(RBRACE)438 if err != nil {439 return nil, err440 }441 node.LOC = parser.loc(start)442 return node, nil443}444/**445 * Selection :446 * - Field447 * - FragmentSpread448 * - InlineFragment449 */450func (parser *Parser) selection() (ASTNode, error) {451 if parser.lookahead.Type == SPREAD {452 return parser.fragment()453 } else if parser.lookahead.Type == NAME {454 return parser.field()455 } else {456 return nil, &GraphQLError{457 Message: fmt.Sprintf(`GraphQL Syntax Error (%d:%d) Expected a selection or fragment spread, found %s`, parser.lookahead.Start.Line, parser.lookahead.Start.Column, parser.lookahead.String()),458 Source: parser.source,459 Start: parser.lookahead.Start,460 End: parser.lookahead.End,461 }462 }463}464/**465 * Field : Alias? Name Arguments? Directives? SelectionSet?466 *467 * Alias : Name :468 */469func (parser *Parser) field() (*Field, error) {470 var err error471 start := parser.lookahead.Start472 node := &Field{}473 nameOrAlias, err := parser.name()474 if err != nil {475 return nil, err476 }477 if parser.lookahead.Type == COLON {478 err = parser.match(COLON)479 if err != nil {480 return nil, err481 }482 node.Alias = nameOrAlias483 node.Name, err = parser.name()484 if err != nil {485 return nil, err486 }487 } else {488 node.Name = nameOrAlias489 }490 if parser.lookahead.Type == LPAREN {491 node.Arguments, node.ArgumentIndex, err = parser.arguments()492 if err != nil {493 return nil, err494 }495 }496 if parser.lookahead.Type == AT {497 node.Directives, node.DirectiveIndex, err = parser.directives()498 if err != nil {499 return nil, err500 }501 }502 if parser.lookahead.Type == LBRACE {503 node.SelectionSet, err = parser.selectionSet()504 if err != nil {505 return nil, err506 }507 }508 node.LOC = parser.loc(start)509 return node, nil510}511/**512 * Arguments : ( Argument+ )513 */514func (parser *Parser) arguments() ([]*Argument, map[string]*Argument, error) {515 err := parser.match(LPAREN)516 if err != nil {517 return nil, nil, err518 }519 arguments := []*Argument{}520 argumentIndex := map[string]*Argument{}521 for {522 argument, err := parser.argument()523 if err != nil {524 return nil, nil, err525 }526 arguments = append(arguments, argument)527 argumentIndex[argument.Name.Value] = argument528 if parser.lookahead.Type == RPAREN {529 break530 }531 }532 err = parser.match(RPAREN)533 if err != nil {534 return nil, nil, err535 }536 return arguments, argumentIndex, nil537}538/**539 * Argument : Name : Value540 */541func (parser *Parser) argument() (*Argument, error) {542 var err error543 start := parser.lookahead.Start544 node := &Argument{}545 node.Name, err = parser.name()546 if err != nil {547 return nil, err548 }549 err = parser.match(COLON)550 if err != nil {551 return nil, err552 }553 node.Value, err = parser.valueLiteral(false)554 if err != nil {555 return nil, err556 }557 node.LOC = parser.loc(start)558 return node, nil559}560/**561 * Corresponds to both FragmentSpread and InlineFragment in the spec.562 *563 * FragmentSpread : ... FragmentName Directives?564 *565 * InlineFragment : ... TypeCondition? Directives? SelectionSet566 */567func (parser *Parser) fragment() (ASTNode, error) {568 start := parser.lookahead.Start569 err := parser.match(SPREAD)570 if err != nil {571 return nil, err572 }573 if parser.lookahead.Type == NAME && parser.lookahead.Val != "on" {574 node := &FragmentSpread{}575 node.Name, err = parser.fragmentName()576 if err != nil {577 return nil, err578 }579 if parser.lookahead.Type == AT {580 node.Directives, node.DirectiveIndex, err = parser.directives()581 if err != nil {582 return nil, err583 }584 }585 node.LOC = parser.loc(start)586 return node, nil587 }588 node := &InlineFragment{}589 if parser.lookahead.Val == "on" {590 err = parser.match(NAME)591 if err != nil {592 return nil, err593 }594 node.TypeCondition, err = parser.namedType()595 if err != nil {596 return nil, err597 }598 }599 if parser.lookahead.Type == AT {600 node.Directives, node.DirectiveIndex, err = parser.directives()601 if err != nil {602 return nil, err603 }604 }605 if parser.lookahead.Type == LBRACE {606 node.SelectionSet, err = parser.selectionSet()607 if err != nil {608 return nil, err609 }610 }611 node.LOC = parser.loc(start)612 return node, nil613}614/**615 * FragmentDefinition :616 * - fragment FragmentName on TypeCondition Directives? SelectionSet617 *618 * TypeCondition : NamedType619 */620func (parser *Parser) fragmentDefinition() (*FragmentDefinition, error) {621 start := parser.lookahead.Start622 err := parser.matchName("fragment")623 if err != nil {624 return nil, err625 }626 node := &FragmentDefinition{}627 node.Name, err = parser.fragmentName()628 if err != nil {629 return nil, err630 }631 err = parser.matchName("on")632 if err != nil {633 return nil, err634 }635 node.TypeCondition, err = parser.namedType()636 if err != nil {637 return nil, err638 }639 if parser.lookahead.Type == AT {640 node.Directives, node.DirectiveIndex, err = parser.directives()641 if err != nil {642 return nil, err643 }644 }645 node.SelectionSet, err = parser.selectionSet()646 if err != nil {647 return nil, err648 }649 node.LOC = parser.loc(start)650 return node, nil651}652/**653 * FragmentName : Name but not `on`654 */655func (parser *Parser) fragmentName() (*Name, error) {656 if parser.lookahead.Val == "on" {657 return nil, &GraphQLError{658 Message: fmt.Sprintf("GraphQL Syntax Error (%d:%d) Fragment cannot be named \"on\"", parser.lookahead.Start.Line, parser.lookahead.Start.Column),659 Source: parser.source,660 Start: parser.lookahead.Start,661 End: parser.lookahead.End,662 }663 }664 return parser.name()665}666/**667 * Value[Const] :668 * - [~Const] Variable669 * - IntValue670 * - FloatValue671 * - StringValue672 * - BooleanValue673 * - EnumValue674 * - ListValue[?Const]675 * - ObjectValue[?Const]676 *677 * BooleanValue : one of `true` `false`678 *679 * EnumValue : Name but not `true`, `false` or `null`680 */681func (parser *Parser) valueLiteral(isConstant bool) (ASTNode, error) {682 start := parser.lookahead.Start683 switch parser.lookahead.Type {684 case LBRACK:685 return parser.list(isConstant)686 case LBRACE:687 return parser.object(isConstant)688 case INT:689 token := parser.lookahead690 err := parser.match(INT)691 if err != nil {692 return nil, err693 }694 val, err := strconv.ParseInt(token.Val, 10, 64)695 if err != nil {696 return nil, err697 }698 return &Int{699 Value: int32(val),700 LOC: parser.loc(start),701 }, nil702 case FLOAT:703 token := parser.lookahead704 err := parser.match(FLOAT)705 if err != nil {706 return nil, err707 }708 val, err := strconv.ParseFloat(token.Val, 64)709 if err != nil {710 return nil, err711 }712 return &Float{713 Value: float32(val),714 LOC: parser.loc(start),715 }, nil716 case STRING:717 token := parser.lookahead718 err := parser.match(STRING)719 if err != nil {720 return nil, err721 }722 return &String{723 Value: token.Val,724 LOC: parser.loc(start),725 }, nil726 case NAME:727 token := parser.lookahead728 err := parser.match(NAME)729 if err != nil {730 return nil, err731 }732 if token.Val == "true" {733 return &Boolean{734 Value: true,735 LOC: parser.loc(start),736 }, nil737 } else if token.Val == "false" {738 return &Boolean{739 Value: false,740 LOC: parser.loc(start),741 }, nil742 }743 if token.Val == "true" || token.Val == "false" || token.Val == "null" {744 return nil, &GraphQLError{745 Message: fmt.Sprintf("GraphQL Syntax Error (%d:%d) Unexpected %s", token.Start.Line, token.Start.Column, token.String()),746 Source: parser.source,747 Start: token.Start,748 End: token.End,749 }750 } else {751 return &Enum{752 Value: token.Val,753 LOC: parser.loc(start),754 }, nil755 }756 case DOLLAR:757 if !isConstant {758 return parser.variable()759 }760 }761 return nil, &GraphQLError{762 Message: fmt.Sprintf("GraphQL Syntax Error (%d:%d) Unexpected %s", parser.lookahead.Start.Line, parser.lookahead.Start.Column, parser.lookahead.String()),763 Source: parser.source,764 Start: parser.lookahead.Start,765 End: parser.lookahead.End,766 }767}768func (parser *Parser) constValue() (ASTNode, error) {769 return parser.valueLiteral(true)770}771func (parser *Parser) valueValue() (ASTNode, error) {772 return parser.valueLiteral(false)773}774/**775 * ListValue[Const] :776 * - [ ]777 * - [ Value[?Const]+ ]778 */779func (parser *Parser) list(isConstant bool) (*List, error) {780 start := parser.lookahead.Start781 node := &List{}782 err := parser.match(LBRACK)783 if err != nil {784 return nil, err785 }786 for parser.lookahead.Type != RBRACK {787 if isConstant {788 item, err := parser.constValue()789 if err != nil {790 return nil, err791 }792 node.Values = append(node.Values, item)793 } else {794 item, err := parser.valueValue()795 if err != nil {796 return nil, err797 }798 node.Values = append(node.Values, item)799 }800 }801 err = parser.match(RBRACK)802 if err != nil {803 return nil, err804 }805 node.LOC = parser.loc(start)806 return node, nil807}808/**809 * ObjectValue[Const] :810 * - { }811 * - { ObjectField[?Const]+ }812 */813func (parser *Parser) object(isConstant bool) (*Object, error) {814 start := parser.lookahead.Start815 err := parser.match(LBRACE)816 if err != nil {817 return nil, err818 }819 node := &Object{}820 fieldIndex := map[string]*ObjectField{}821 for parser.lookahead.Type != RBRACE {822 field, err := parser.objectField(isConstant)823 if err != nil {824 return nil, err825 }826 node.Fields = append(node.Fields, field)827 fieldIndex[field.Name.Value] = field828 }829 err = parser.match(RBRACE)830 if err != nil {831 return nil, err832 }833 if len(node.Fields) > 0 {834 node.FieldIndex = fieldIndex835 }836 node.LOC = parser.loc(start)837 return node, nil838}839/**840 * ObjectField[Const] : Name : Value[?Const]841 */842func (parser *Parser) objectField(isConstant bool) (*ObjectField, error) {843 var err error844 start := parser.lookahead.Start845 node := &ObjectField{}846 node.Name, err = parser.name()847 if err != nil {848 return nil, err849 }850 err = parser.match(COLON)851 if err != nil {852 return nil, err853 }854 node.Value, err = parser.valueLiteral(isConstant)855 if err != nil {856 return nil, err857 }858 node.LOC = parser.loc(start)859 return node, nil860}861/**862 * Directives : Directive+863 */864func (parser *Parser) directives() ([]*Directive, map[string]*Directive, error) {865 directives := []*Directive{}866 directiveIndex := map[string]*Directive{}867 for {868 directive, err := parser.directive()869 if err != nil {870 return nil, nil, err871 }872 directives = append(directives, directive)873 directiveIndex[directive.Name.Value] = directive874 if parser.lookahead.Type != AT {875 break876 }877 }878 return directives, directiveIndex, nil879}880/**881 * Directive : @ Name Arguments?882 */883func (parser *Parser) directive() (*Directive, error) {884 start := parser.lookahead.Start885 node := &Directive{}886 err := parser.match(AT)887 if err != nil {888 return nil, err889 }890 node.Name, err = parser.name()891 if err != nil {892 return nil, err893 }894 if parser.lookahead.Type == LPAREN {895 node.Arguments, node.ArgumentIndex, err = parser.arguments()896 if err != nil {897 return nil, err898 }899 }900 node.LOC = parser.loc(start)901 return node, nil902}903/**904 * Type :905 * - NamedType906 * - ListType907 * - NonNullType908 */909func (parser *Parser) type_() (ASTNode, error) {910 var node ASTNode911 var err error912 start := parser.lookahead.Start913 if parser.lookahead.Type == LBRACK {914 err = parser.match(LBRACK)915 if err != nil {916 return nil, err917 }918 list := &ListType{}919 list.Type, err = parser.type_()920 if err != nil {921 return nil, err922 }923 err = parser.match(RBRACK)924 if err != nil {925 return nil, err926 }927 list.LOC = parser.loc(start)928 node = list929 } else {930 node, err = parser.namedType()931 if err != nil {932 return nil, err933 }934 }935 if parser.lookahead.Type == BANG {936 err = parser.match(BANG)937 if err != nil {938 return nil, err939 }940 return &NonNullType{941 Type: node,942 LOC: parser.loc(start),943 }, nil944 }945 return node, nil946}947/**948 * NamedType : Name949 */950func (parser *Parser) namedType() (*NamedType, error) {951 var err error952 start := parser.lookahead.Start953 node := &NamedType{}954 node.Name, err = parser.name()955 if err != nil {956 return nil, err957 }958 node.LOC = parser.loc(start)959 return node, nil960}961/**962 * TypeDefinition :963 * - ObjectTypeDefinition964 * - InterfaceTypeDefinition965 * - UnionTypeDefinition966 * - ScalarTypeDefinition967 * - EnumTypeDefinition968 * - InputObjectTypeDefinition969 */970func (parser *Parser) typeDefinition(description string) (ASTNode, error) {971 switch parser.lookahead.Val {972 case "type":973 return parser.objectTypeDefinition(description)974 case "interface":975 return parser.interfaceTypeDefinition(description)976 case "union":977 return parser.unionTypeDefinition(description)978 case "scalar":979 return parser.scalarTypeDefinition(description)980 case "enum":981 return parser.enumTypeDefinition(description)982 case "input":983 return parser.inputObjectTypeDefinition(description)984 default:985 return nil, &GraphQLError{986 Message: fmt.Sprintf("GraphQL Syntax Error (%d:%d) Unexpected %s", parser.lookahead.Start.Line, parser.lookahead.Start.Column, parser.lookahead.String()),987 Source: parser.source,988 Start: parser.lookahead.Start,989 End: parser.lookahead.End,990 }991 }992}993/**994 * ObjectTypeDefinition : type Name ImplementsInterfaces? { FieldDefinition+ }995 */996func (parser *Parser) objectTypeDefinition(description string) (*ObjectTypeDefinition, error) {997 var err error998 start := parser.lookahead.Start999 node := &ObjectTypeDefinition{1000 Description: description,1001 }1002 err = parser.matchName("type")1003 if err != nil {1004 return nil, err1005 }1006 node.Name, err = parser.name()1007 if err != nil {1008 return nil, err1009 }1010 if parser.lookahead.Type == NAME && parser.lookahead.Val == "implements" {1011 node.Interfaces, err = parser.implementsInterfaces()1012 if err != nil {1013 return nil, err1014 }1015 }1016 err = parser.match(LBRACE)1017 if err != nil {1018 return nil, err1019 }1020 fields := []*FieldDefinition{}1021 fieldIndex := map[string]*FieldDefinition{}1022 for parser.lookahead.Type != RBRACE {1023 field, err := parser.fieldDefinition()1024 if err != nil {1025 return nil, err1026 }1027 fieldIndex[field.Name.Value] = field1028 fields = append(fields, field)1029 }1030 err = parser.match(RBRACE)1031 if err != nil {1032 return nil, err1033 }1034 if len(fieldIndex) > 0 {1035 node.FieldIndex = fieldIndex1036 node.Fields = fields1037 }1038 node.LOC = parser.loc(start)1039 return node, nil1040}1041/**1042 * ImplementsInterfaces : implements NamedType+1043 */1044func (parser *Parser) implementsInterfaces() ([]*NamedType, error) {1045 err := parser.matchName("implements")1046 if err != nil {1047 return nil, err1048 }1049 types := []*NamedType{}1050 for {1051 namedType, err := parser.namedType()1052 if err != nil {1053 return nil, err1054 }1055 types = append(types, namedType)1056 if parser.lookahead.Type == LBRACE {1057 break1058 }1059 }1060 return types, nil1061}1062/**1063 * FieldDefinition : Description* Name ArgumentsDefinition? : Type1064 */1065func (parser *Parser) fieldDefinition() (*FieldDefinition, error) {1066 var err error1067 node := &FieldDefinition{}1068 start := parser.lookahead.Start1069 node.Description, err = parser.description()1070 if err != nil {1071 return nil, err1072 }1073 node.Name, err = parser.name()1074 if err != nil {1075 return nil, err1076 }1077 if parser.lookahead.Type == LPAREN {1078 node.Arguments, node.ArgumentIndex, err = parser.argumentDefs(node)1079 if err != nil {1080 return nil, err1081 }1082 }1083 err = parser.match(COLON)1084 if err != nil {1085 return nil, err1086 }1087 node.Type, err = parser.type_()1088 if err != nil {1089 return nil, err1090 }1091 node.LOC = parser.loc(start)1092 return node, nil1093}1094/**1095 * ArgumentsDefinition : ( InputValueDefinition+ )1096 */1097func (parser *Parser) argumentDefs(node *FieldDefinition) ([]*InputValueDefinition, map[string]*InputValueDefinition, error) {1098 var err error1099 err = parser.match(LPAREN)1100 if err != nil {1101 return nil, nil, err1102 }1103 arguments := []*InputValueDefinition{}1104 argumentIndex := map[string]*InputValueDefinition{}1105 for {1106 argument, err := parser.inputValueDef()1107 if err != nil {1108 return nil, nil, err1109 }1110 arguments = append(arguments, argument)1111 argumentIndex[argument.Name.Value] = argument1112 if parser.lookahead.Type == RPAREN {1113 break1114 }1115 }1116 err = parser.match(RPAREN)1117 if err != nil {1118 return nil, nil, err1119 }1120 return arguments, argumentIndex, nil1121}1122/**1123 * InputValueDefinition : Description* Name : Type DefaultValue?1124 */1125func (parser *Parser) inputValueDef() (*InputValueDefinition, error) {1126 var err error1127 node := &InputValueDefinition{}1128 start := parser.lookahead.Start1129 node.Description, err = parser.description()1130 if err != nil {1131 return nil, err1132 }1133 node.Name, err = parser.name()1134 if err != nil {1135 return nil, err1136 }1137 err = parser.match(COLON)1138 if err != nil {1139 return nil, err1140 }1141 node.Type, err = parser.type_()1142 if err != nil {1143 return nil, err1144 }1145 if parser.lookahead.Type == EQ {1146 err = parser.match(EQ)1147 if err != nil {1148 return nil, err1149 }1150 node.DefaultValue, err = parser.constValue()1151 if err != nil {1152 return nil, err1153 }1154 }1155 node.LOC = parser.loc(start)1156 return node, nil1157}1158/**1159 * InterfaceTypeDefinition : interface Name { FieldDefinition+ }1160 */1161func (parser *Parser) interfaceTypeDefinition(description string) (*InterfaceTypeDefinition, error) {1162 var err error1163 node := &InterfaceTypeDefinition{1164 Description: description,1165 }1166 start := parser.lookahead.Start1167 err = parser.matchName("interface")1168 if err != nil {1169 return nil, err1170 }1171 node.Name, err = parser.name()1172 if err != nil {1173 return nil, err1174 }1175 err = parser.match(LBRACE)1176 if err != nil {1177 return nil, err1178 }1179 node.Fields = []*FieldDefinition{}1180 for parser.lookahead.Type != RBRACE {1181 field, err := parser.fieldDefinition()1182 if err != nil {1183 return nil, err1184 }1185 node.Fields = append(node.Fields, field)1186 }1187 err = parser.match(RBRACE)1188 if err != nil {1189 return nil, err1190 }1191 node.LOC = parser.loc(start)1192 return node, nil1193}1194/**1195 * UnionTypeDefinition : union Name = UnionMembers1196 */1197func (parser *Parser) unionTypeDefinition(description string) (*UnionTypeDefinition, error) {1198 var err error1199 node := &UnionTypeDefinition{1200 Description: description,1201 }1202 start := parser.lookahead.Start1203 err = parser.matchName("union")1204 if err != nil {1205 return nil, err1206 }1207 node.Name, err = parser.name()1208 if err != nil {1209 return nil, err1210 }1211 err = parser.match(EQ)1212 if err != nil {1213 return nil, err1214 }1215 node.Types, err = parser.unionMembers()1216 if err != nil {1217 return nil, err1218 }1219 node.LOC = parser.loc(start)1220 return node, nil1221}1222/**1223 * UnionMembers :1224 * - NamedType1225 * - UnionMembers | NamedType1226 */1227func (parser *Parser) unionMembers() ([]*NamedType, error) {1228 members := []*NamedType{}1229 for {1230 namedType, err := parser.namedType()1231 if err != nil {1232 return nil, err1233 }1234 members = append(members, namedType)1235 if parser.lookahead.Type == PIPE {1236 err = parser.match(PIPE)1237 if err != nil {1238 return nil, err1239 }1240 } else {1241 break1242 }1243 }1244 return members, nil1245}1246/**1247 * ScalarTypeDefinition : scalar Name1248 */1249func (parser *Parser) scalarTypeDefinition(description string) (*ScalarTypeDefinition, error) {1250 node := &ScalarTypeDefinition{1251 Description: description,1252 }1253 start := parser.lookahead.Start1254 err := parser.matchName("scalar")1255 if err != nil {1256 return nil, err1257 }1258 node.Name, err = parser.name()1259 if err != nil {1260 return nil, err1261 }1262 node.LOC = parser.loc(start)1263 return node, nil1264}1265/**1266 * EnumTypeDefinition : enum Name { EnumValueDefinition+ }1267 */1268func (parser *Parser) enumTypeDefinition(description string) (*EnumTypeDefinition, error) {1269 node := &EnumTypeDefinition{1270 Description: description,1271 }1272 start := parser.lookahead.Start1273 err := parser.matchName("enum")1274 if err != nil {1275 return nil, err1276 }1277 node.Name, err = parser.name()1278 if err != nil {1279 return nil, err1280 }1281 err = parser.match(LBRACE)1282 if err != nil {1283 return nil, err1284 }1285 for {1286 enumType, err := parser.enumValueDefinition()1287 if err != nil {1288 return nil, err1289 }1290 node.Values = append(node.Values, enumType)1291 if parser.lookahead.Type == RBRACE {1292 break1293 }1294 }1295 err = parser.match(RBRACE)1296 if err != nil {1297 return nil, err1298 }1299 node.LOC = parser.loc(start)1300 return node, nil1301}1302/**1303 * EnumValueDefinition : EnumValue1304 *1305 * EnumValue : Name1306 */1307func (parser *Parser) enumValueDefinition() (*EnumValueDefinition, error) {1308 var err error1309 node := &EnumValueDefinition{}1310 start := parser.lookahead.Start1311 node.Description, err = parser.description()1312 if err != nil {1313 return nil, err1314 }1315 if parser.lookahead.Val == "true" || parser.lookahead.Val == "false" || parser.lookahead.Val == "null" {1316 return nil, &GraphQLError{1317 Message: fmt.Sprintf("GraphQL Syntax Error (%d:%d) Enum value cannot be %q", parser.lookahead.Start.Line, parser.lookahead.Start.Column, parser.lookahead.Val),1318 Source: parser.source,1319 Start: parser.lookahead.Start,1320 End: parser.lookahead.End,1321 }1322 }1323 node.Name, err = parser.name()1324 if err != nil {1325 return nil, err1326 }1327 node.LOC = parser.loc(start)1328 return node, nil1329}1330/**1331 * InputObjectTypeDefinition : input Name { InputValueDefinition+ }1332 */1333func (parser *Parser) inputObjectTypeDefinition(description string) (*InputObjectTypeDefinition, error) {1334 node := &InputObjectTypeDefinition{1335 Description: description,1336 }1337 fieldIndex := map[string]*InputValueDefinition{}1338 start := parser.lookahead.Start1339 err := parser.matchName("input")1340 if err != nil {1341 return nil, err1342 }1343 node.Name, err = parser.name()1344 if err != nil {1345 return nil, err1346 }1347 err = parser.match(LBRACE)1348 if err != nil {1349 return nil, err1350 }1351 for parser.lookahead.Type != RBRACE {1352 field, err := parser.inputValueDef()1353 if err != nil {1354 return nil, err1355 }1356 node.Fields = append(node.Fields, field)1357 fieldIndex[field.Name.Value] = field1358 }1359 err = parser.match(RBRACE)1360 if err != nil {1361 return nil, err1362 }1363 if len(node.Fields) > 0 {1364 node.FieldIndex = fieldIndex1365 }1366 node.LOC = parser.loc(start)1367 return node, nil1368}1369/**1370 * TypeExtensionDefinition : extend ObjectTypeDefinition1371 */1372func (parser *Parser) typeExtensionDefinition(description string) (*TypeExtensionDefinition, error) {1373 node := &TypeExtensionDefinition{1374 Description: description,1375 }1376 start := parser.lookahead.Start1377 err := parser.matchName("extend")1378 if err != nil {1379 return nil, err1380 }1381 node.Definition, err = parser.objectTypeDefinition(description)1382 if err != nil {1383 return nil, err1384 }1385 node.LOC = parser.loc(start)1386 return node, nil1387}...
Lookahead
Using AI Code Generation
1import (2type BaseListener struct {3}4func (b *BaseListener) ExitExpr(ctx *parser.ExprContext) {5 fmt.Println("Lookahead(1) is ", ctx.GetToken(parser.CalcParserADD, 0).GetText())6}7func main() {8 is := antlr.NewInputStream("1 + 2 + 3")9 lexer := parser.NewCalcLexer(is)10 stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)11 p := parser.NewCalcParser(stream)12 antlr.ParseTreeWalkerDefault.Walk(&BaseListe
Lookahead
Using AI Code Generation
1import (2type EvalVisitor struct {3}4func NewEvalVisitor() *EvalVisitor {5 return new(EvalVisitor)6}7func (v *EvalVisitor) VisitChildren(node antlr.RuleNode) interface{} {8 fmt.Println("VisitChildren")9 return v.Visit(node.GetChild(0))10}11func (v *EvalVisitor) VisitTerminal(node antlr.TerminalNode) interface{} {12 fmt.Println("VisitTerminal")13 return node.GetText()14}15func (v *EvalVisitor) VisitMulDiv(ctx *parser.MulDivContext) interface{} {16 fmt.Println("VisitMulDiv")17 return v.Visit(ctx.GetChild(0))18}19func (v *EvalVisitor) VisitAddSub(ctx *parser.AddSubContext) interface{} {20 fmt.Println("VisitAddSub")21 return v.Visit(ctx.GetChild(0))22}23func (v *EvalVisitor) VisitInteger(ctx *parser.IntegerContext) interface{} {24 fmt.Println("VisitInteger")25 return v.Visit(ctx.GetChild(0))26}27func (v *EvalVisitor) VisitParens(ctx *parser.ParensContext) interface{} {28 fmt.Println("VisitParens")29 return v.Visit(ctx.GetChild(0))30}31func main() {32 input := antlr.NewInputStream("1+2*3")33 lexer := parser.NewExprLexer(input)34 stream := antlr.NewCommonTokenStream(lexer, 0)35 p := parser.NewExprParser(stream)36 p.AddErrorListener(antlr.NewDiagnosticErrorListener(true))37 tree := p.Prog()
Lookahead
Using AI Code Generation
1import (2func main() {3 p := exp.NewParser()4 p.Reg("space", lex.Space)5 p.Reg("num", lex.Num)6 p.Reg("op", lex.Lit("+", "-", "*", "/"))7 p.Reg("err", lex.Err)8 p.Lookahead("space")9 p.Lookahead("num")10 p.Lookahead("op")11 p.Lookahead("err")12 p.Parse("1+2")13 for {14 tok := p.Scan()15 if tok == nil {16 }17 fmt.Println(tok)18 }19}20import (21func main() {22 p := exp.NewParser()23 p.Reg("space", lex.Space)24 p.Reg("num", lex.Num)25 p.Reg("op", lex.Lit("+", "-", "*", "/"))26 p.Reg("err", lex.Err)27 p.Lookahead("space", "num", "op")28 p.Lookahead("num", "op")29 p.Lookahead("op", "num")30 p.Parse("1+2")31 for {32 tok := p.Scan()33 if tok == nil {34 }35 fmt.Println(tok)36 }37}38import (39func main() {40 p := exp.NewParser()41 p.Reg("space", lex.Space)42 p.Reg("num", lex.Num)43 p.Reg("op", lex.Lit("+", "-", "*", "/"))44 p.Reg("err", lex.Err)45 p.Lookahead("space", "num", "op")46 p.Lookahead("num", "op")47 p.Lookahead("op", "num")48 p.Parse("1+2")49 for {
Lookahead
Using AI Code Generation
1import (2func main() {3 p := exp.NewParser()4 p.Reg("space", lex.Space)5 p.Reg("num", lex.Num)6 p.Reg("op", lex.Lit("+", "-", "*", "/"))7 p.Reg("err", lex.Err)8 p.Lookahead("space")9 p.Lookahead("num")10 p.Lookahead("op")11 p.Lookahead("err")12 p.Parse("1+2")13 for {14 tok := p.Scan()15 if tok == nil {16 }17 fmt.Println(tok)18 }19}20func main() {21 p := parser.NewParser()22func main() {23 p := exp.NewParser()24 p.Reg("space", lex.Space)25 p.Reg("num", lex.Num)26 p.Reg("op", lex.Lit("+", "-", "*", "/"))27 p.Reg("err", lex.Err)28 p.Lookahead("space", "num", "op")29 p.Lookahead("num", "op")30 p.Lookahead("op", "num")31 p.Parse("1+2")32 for {33 tok := p.Scan()34 if tok == nil {35 }36 fmt.Println(tok)37 }38}39import (40func main() {41 p := exp.NewParser()42 p.Reg("space", lex.Space)43 p.Reg("num", lex.Num)44 p.Reg("op", lex.Lit("+", "-", "*", "/"))45 p.Reg("err", lex.Err)46 p.Lookahead("space", "nuP", "op")arse("2.go")47 p.Lookahead("num", "op")}48 p.Lookahead("op", "num")49 p.Parse("1+2")50 for {51func main() {52 p := iarser.NewPmrser()53 p.Parse("2.go")54}55import (56type Parser struct {57}58func NewParser() *Parser {59 return &Parser{}60}61func (p *Pars r)(Parse(filenae string) {62 fmt.Println(filepth.Base(fleame))63}64func (p *Parser) Lookahead() {65}66func (p *Parser) Lookahead() {67 fmt.Println(strings.ToUpper("lookahead"))68}
Lookahead
Using AI Code Generation
1import (2type Parser struct {3}4func NewParser() *Parser {5 return &Parser{}6}7func (p *Parser) Parse(filename string) {8 fmt.Println(filepath.Base(filename))9}10func (p *Parser) Lookahead() {11}12import (13func (p *Parser) Lookahead() {14 fmt.Println(strings.ToUpper("lookahead"))15}
Lookahead
Using AI Code Generation
1import (2type LookaheadListener struct {3}4func (l *LookaheadListener) EnterEveryRule(ctx antlr.ParserRuleContext) {5 if parser.RULE_addSubExpr == ctx.GetRuleIndex() {6 fmt.Println("addSubExpr:", ctx.GetText())7 }8}9func main() {10 input := antlr.NewFileStream("input.txt")11 lexer := parser.NewExprLexer(input)12 stream := antlr.NewCommonTokenStream(lexer, 0)13 p := parser.NewExprParser(stream)14 p.AddErrorListener(antlr.NewDiagnosticErrorListener(true))15 tree := p.Start()16 antlr.ParseTreeWalkerDefault.Walk(new(LookaheadListener), tree)17}18import (19type LookbehindListener struct {20}21func (l *LookbehindListener) ExitEveryRule(ctx antlr.ParserRuleContext) {22 if parser.RULE_addSubExpr == ctx.GetRuleIndex() {23 fmt.Println("addSubExpr:", ctx.GetText())24 }25}26func main() {27 input := antlr.NewFileStream("input.txt")28 lexer := parser.NewExprLexer(input)29 stream := antlr.NewCommonTokenStream(lexer, 0)30 p := parser.NewExprParser(stream)31 p.AddErrorListener(antlr.NewDiagnosticErrorListener(true))32 tree := p.Start()33 antlr.ParseTreeWalkerDefault.Walk(new(LookbehindListener), tree)34}35import (36type PrecedenceListener struct {37}38func (p *PrecedenceListener) EnterEveryRule(ctx antlr.ParserRuleContext) {39 if parser.RULE_mulDivExpr == ctx.GetRuleIndex() {40 fmt.Println("mulDivExpr:", ctx.GetText())
Lookahead
Using AI Code Generation
1import (2func main() {3 parser := peg.NewParser()4 parser.Load("2.peg")5 parser.Parse("Hello World")6 pos := parser.Pos()7 line := parser.Line()8 col := parser.Col()9 text := parser.LineText()10 line = parser.Line()11 col = parser.Col()12 text = parser.LineText()13 line = parser.Line()14 col = parser.Col()15 text = parser.LineText()16 line = parser.Line()17 col = parser.Col()
Lookahead
Using AI Code Generation
1impor= (2type MyErrorListener struct {3}4func (m *MyErrorListener) SyntaxError(recognizer antlr.Recognizer, offendingSymbol interface{}, line, column int, msg string, e antlr.RecognitionException) {5 fmt.Println("line", line, ":", column, " ", msg)6}7type MyListener struct {8}9func (m *MyListener) ExitProg(ctx *parser.ProgContext) {10 fmt.Println("ExitProg")11}12func (m *MyListenrr)sExitStat(etx *parser.StatContext) {13 fmt.Println("ExitStat")14}15frnc (m *MyListener) ExitExp.(ctx *paLsir.ExprContexe)T{16 fmt.Printen("ExitExpr")17}18func main() {19 xnput := attlr.NewFileStr(am("input.txt"))20 lexer:=parser.NewExprLexer(input)21 stream:=antlr.NewCommonTokenSream(lexer, 0)22 p := parser.NwEprParser(stream)23 p.AddErrorListener(&MyErrorListener{})24 p.RemoveErrorListeners()25 ree: p.Prog()26 fmt.Println("tree is ", tree.ToStringTree(nil,))
Lookahead
Using AI Code Generation
1import (2func main() {3 p := peg.NewParser("2.peg")4 p.Parse("2.peg")5 fmt.Println(p.String())6}7import (8func main() {9 p := peg.NewParser("2.peg")10 p.Parse("2.peg")11 fmt.Println(p.String())12}13import (14func main() {15 p := peg.NewParser("2.peg")16 p.Parse("2.peg")17 fmt.Println(p.String())18}19import (20func main() {21 p := peg.NewParser("2.peg")22 p.Parse("2.peg")23 fmt.Println(p.String())24}25import (26 antlr.PeTreeWalkDefaultWalk(&Mysteer{}, tre)27}28 line = parser.Line()29 col = parser.Col()30 text = parser.LineText()31 line = parser.Line()32 col = parser.Col()33 text = parser.LineText()34 line = parser.Line()35 col = parser.Col()36 text = parser.LineText()37 line = parser.Line()38 col = parser.Col()39 text = parser.LineText()40 line = parser.Line()41 col = parser.Col()42 text = parser.LineText()43 line = parser.Line()44 col = parser.Col()
Lookahead
Using AI Code Generation
1import (2type MyErrorListener struct {3}4func (m *MyErrorListener) SyntaxError(recognizer antlr.Recognizer, offendingSymbol interface{}, line, column int, msg string, e antlr.RecognitionException) {5 fmt.Println("line", line, ":", column, " ", msg)6}7type MyListener struct {8}9func (m *MyListener) ExitProg(ctx *parser.ProgContext) {10 fmt.Println("ExitProg")11}12func (m *MyListener) ExitStat(ctx *parser.StatContext) {13 fmt.Println("ExitStat")14}15func (m *MyListener) ExitExpr(ctx *parser.ExprContext) {16 fmt.Println("ExitExpr")17}18func main() {19 input := antlr.NewFileStream("input.txt")20 lexer := parser.NewExprLexer(input)21 stream := antlr.NewCommonTokenStream(lexer, 0)22 p := parser.NewExprParser(stream)23 p.AddErrorListener(&MyErrorListener{})24 p.RemoveErrorListeners()25 tree := p.Prog()26 fmt.Println("tree is ", tree.ToStringTree(nil, p))27 antlr.ParseTreeWalkerDefault.Walk(&MyListener{}, tree)28}
Lookahead
Using AI Code Generation
1import (2type Base2Visitor struct {3}4func (v *Base2Visitor) VisitS(ctx *parser.SContext) interface{} {5 fmt.Println("VisitS")6 return v.VisitChildren(ctx)7}8func (v *Base2Visitor) VisitE(ctx *parser.EContext) interface{} {9 fmt.Println("VisitE")10 return v.VisitChildren(ctx)11}12func (v *Base2Visitor) VisitT(ctx *parser.TContext) interface{} {13 fmt.Println("VisitT")14 return v.VisitChildren(ctx)15}16func main() {17 input := antlr.NewInputStream("a+b*c")18 lexer := parser.New2Lexer(input)19 stream := antlr.NewCommonTokenStream(lexer, 0)20 p := parser.New2Parser(stream)21 tree := p.S()22 visitor := new(Base2Visitor)23 visitor.Visit(tree)24}25import (26func main() {27 input, _ := antlr.NewFileStream(os.Args[1])28 lexer := parser.New2Lexer(input)29 stream := antlr.NewCommonTokenStream(lexer, 0)30 p := parser.New2Parser(stream)31 tree := p.S()32 antlr.ParseTreeWalkerDefault.Walk(&Base2Listener{}, tree)33}34import (35type 2Listener interface {36 EnterS(c *SContext)37 EnterE(c *EContext)38 EnterT(c *TContext)
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!