Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(288)

Side by Side Diff: mojom/mojom_parser/parser/parsing.go

Issue 1511353002: New Mojom parser: Fix error message for invalid enum value initailizers. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojom/mojom_parser/mojom/types.go ('k') | mojom/mojom_parser/parser/resolution_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 package parser 1 package parser
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 "math" 5 "math"
6 "mojom/mojom_parser/lexer" 6 "mojom/mojom_parser/lexer"
7 "mojom/mojom_parser/mojom" 7 "mojom/mojom_parser/mojom"
8 "strconv" 8 "strconv"
9 ) 9 )
10 10
(...skipping 921 matching lines...) Expand 10 before | Expand all | Expand 10 after
932 p.lastConsumed, nextToken) 932 p.lastConsumed, nextToken)
933 p.parseErrorT(ParserErrorCodeUnexpectedToken, me ssage, p.lastConsumed) 933 p.parseErrorT(ParserErrorCodeUnexpectedToken, me ssage, p.lastConsumed)
934 return false 934 return false
935 } 935 }
936 firstValue = false 936 firstValue = false
937 name := p.readName() 937 name := p.readName()
938 p.attachToken() 938 p.attachToken()
939 nameToken := p.lastConsumed 939 nameToken := p.lastConsumed
940 var valueRef mojom.ValueRef 940 var valueRef mojom.ValueRef
941 if p.tryMatch(lexer.Equals) { 941 if p.tryMatch(lexer.Equals) {
942 » » » » valueRef = p.parseEnumValueInitializer(mojomEnum ) 942 » » » » valueRef = p.parseEnumValueInitializer(mojomEnum , name)
943 } 943 }
944 declData := p.DeclData(name, nameToken, attributes) 944 declData := p.DeclData(name, nameToken, attributes)
945 duplicateNameError = mojomEnum.AddEnumValue(declData, va lueRef) 945 duplicateNameError = mojomEnum.AddEnumValue(declData, va lueRef)
946 trailingCommaFound = p.tryMatch(lexer.Comma) 946 trailingCommaFound = p.tryMatch(lexer.Comma)
947 case lexer.RBrace: 947 case lexer.RBrace:
948 rbraceFound = true 948 rbraceFound = true
949 if attributes != nil { 949 if attributes != nil {
950 message := "Enum body ends with extraneouss attr ibutes." 950 message := "Enum body ends with extraneouss attr ibutes."
951 p.parseError(ParserErrorCodeBadAttributeLocation , message) 951 p.parseError(ParserErrorCodeBadAttributeLocation , message)
952 } 952 }
953 break 953 break
954 case lexer.Comma: 954 case lexer.Comma:
955 break 955 break
956 default: 956 default:
957 p.unexpectedTokenError(nextToken, "either another enum v alue or }") 957 p.unexpectedTokenError(nextToken, "either another enum v alue or }")
958 return false 958 return false
959 } 959 }
960 if p.OK() && duplicateNameError != nil { 960 if p.OK() && duplicateNameError != nil {
961 p.err = duplicateNameError 961 p.err = duplicateNameError
962 return false 962 return false
963 } 963 }
964 } 964 }
965 return p.OK() 965 return p.OK()
966 } 966 }
967 967
968 // ENUM_VAL_INITIALIZER -> INTEGER_LITERAL | ENUM_VALUE_REF 968 // ENUM_VAL_INITIALIZER -> INTEGER_LITERAL | ENUM_VALUE_REF
969 // ENUM_VALUE_REF -> IDENTIFIER {{that resolves to a declared enum value}} 969 // ENUM_VALUE_REF -> IDENTIFIER {{that resolves to a declared enum value}}
970 func (p *Parser) parseEnumValueInitializer(mojoEnum *mojom.MojomEnum) mojom.Valu eRef { 970 func (p *Parser) parseEnumValueInitializer(mojoEnum *mojom.MojomEnum, valueName string) mojom.ValueRef {
971 if !p.OK() { 971 if !p.OK() {
972 return nil 972 return nil
973 } 973 }
974 p.pushChildNode("enumValueInitializer") 974 p.pushChildNode("enumValueInitializer")
975 defer p.popNode() 975 defer p.popNode()
976 976
977 // We need to manufacture an instance of Type to act as the "assigneeTyp e" 977 // We need to manufacture an instance of Type to act as the "assigneeTyp e"
978 // for the new ValueSpec we are creating. This is because unlike 978 // for the new ValueSpec we are creating. This is because unlike
979 // other types of value assignment, an enum value initializer is not 979 // other types of value assignment, an enum value initializer is not
980 // preceded by a type reference for the assignee. Rather the type of 980 // preceded by a type reference for the assignee. Rather the type of
981 // the assignee is implicit in the scope. 981 // the assignee is implicit in the scope.
982 enumType := mojom.NewResolvedUserTypeRef(mojoEnum.FullyQualifiedName(), mojoEnum) 982 enumType := mojom.NewResolvedUserTypeRef(mojoEnum.FullyQualifiedName(), mojoEnum)
983 983
984 valueToken := p.peekNextToken("Parsing an enum value initializer type.") 984 valueToken := p.peekNextToken("Parsing an enum value initializer type.")
985 » valueRef := p.parseValue(mojom.AssigneeSpec{"enum value", enumType}) 985 » valueRef := p.parseValue(mojom.AssigneeSpec{valueName, enumType})
986 if valueRef == nil { 986 if valueRef == nil {
987 return nil 987 return nil
988 } 988 }
989 if !valueRef.MarkUsedAsEnumValueInitializer() { 989 if !valueRef.MarkUsedAsEnumValueInitializer() {
990 message := fmt.Sprintf("Illegal value: %s. An enum value initial izer must be a signed 32-bit integer value.", 990 message := fmt.Sprintf("Illegal value: %s. An enum value initial izer must be a signed 32-bit integer value.",
991 valueToken) 991 valueToken)
992 p.parseErrorT(ParserErrorCodeUnexpectedToken, message, valueToke n) 992 p.parseErrorT(ParserErrorCodeUnexpectedToken, message, valueToke n)
993 return nil 993 return nil
994 } 994 }
995 995
(...skipping 698 matching lines...) Expand 10 before | Expand all | Expand 10 after
1694 func (p *Parser) unexpectedTokenError(token lexer.Token, expected string) { 1694 func (p *Parser) unexpectedTokenError(token lexer.Token, expected string) {
1695 var message string 1695 var message string
1696 switch token.Kind { 1696 switch token.Kind {
1697 case lexer.ErrorUnterminatedStringLiteral, lexer.ErrorUnterminatedCommen t: 1697 case lexer.ErrorUnterminatedStringLiteral, lexer.ErrorUnterminatedCommen t:
1698 message = fmt.Sprintf("%s", token) 1698 message = fmt.Sprintf("%s", token)
1699 default: 1699 default:
1700 message = fmt.Sprintf("Unexpected %s. Expecting %s.", token, exp ected) 1700 message = fmt.Sprintf("Unexpected %s. Expecting %s.", token, exp ected)
1701 } 1701 }
1702 p.parseErrorT(ParserErrorCodeUnexpectedToken, message, token) 1702 p.parseErrorT(ParserErrorCodeUnexpectedToken, message, token)
1703 } 1703 }
OLDNEW
« no previous file with comments | « mojom/mojom_parser/mojom/types.go ('k') | mojom/mojom_parser/parser/resolution_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698