@@ -8,14 +8,42 @@ import (
88 "github.com/sourcegraph/sourcegraph/lib/errors"
99)
1010
11+ // IsGlobalSymbol returns true if the symbol is obviously not a local symbol.
12+ //
13+ // CAUTION: Does not perform full validation of the symbol string's contents.
1114func IsGlobalSymbol (symbol string ) bool {
1215 return ! IsLocalSymbol (symbol )
1316}
1417
18+ // IsLocalSymbol returns true if the symbol is obviously not a global symbol.
19+ //
20+ // CAUTION: Does not perform full validation of the symbol string's contents.
1521func IsLocalSymbol (symbol string ) bool {
1622 return strings .HasPrefix (symbol , "local " )
1723}
1824
25+ func isSimpleIdentifier (s string ) bool {
26+ for _ , c := range s {
27+ if ('0' <= c && c <= '9' ) || ('a' <= c && c <= 'z' ) || ('A' <= c && c <= 'Z' ) ||
28+ c == '$' || c == '+' || c == '-' || c == '_' {
29+ continue
30+ }
31+ return false
32+ }
33+ return true
34+ }
35+
36+ func tryParseLocalSymbol (symbol string ) (string , error ) {
37+ if ! strings .HasPrefix (symbol , "local " ) {
38+ return "" , nil
39+ }
40+ suffix := symbol [6 :]
41+ if len (suffix ) > 0 && isSimpleIdentifier (suffix ) {
42+ return suffix , nil
43+ }
44+ return "" , errors .Newf ("expected format 'local <simple-identifier>' but got: %v" , symbol )
45+ }
46+
1947// ParseSymbol parses an SCIP string into the Symbol message.
2048func ParseSymbol (symbol string ) (* Symbol , error ) {
2149 return ParsePartialSymbol (symbol , true )
@@ -24,14 +52,18 @@ func ParseSymbol(symbol string) (*Symbol, error) {
2452// ParsePartialSymbol parses an SCIP string into the Symbol message
2553// with the option to exclude the `.Descriptor` field.
2654func ParsePartialSymbol (symbol string , includeDescriptors bool ) (* Symbol , error ) {
55+ local , err := tryParseLocalSymbol (symbol )
56+ if err != nil {
57+ return nil , err
58+ }
59+ if local != "" {
60+ return newLocalSymbol (local ), nil
61+ }
2762 s := newSymbolParser (symbol )
2863 scheme , err := s .acceptSpaceEscapedIdentifier ("scheme" )
2964 if err != nil {
3065 return nil , err
3166 }
32- if scheme == "local" {
33- return newLocalSymbol (string (s .Symbol [s .index :])), nil
34- }
3567 manager , err := s .acceptSpaceEscapedIdentifier ("package manager" )
3668 if err != nil {
3769 return nil , err
0 commit comments