Skip to content

Commit

Permalink
Fix lexer to identify single-quote string that immediately occurs aft…
Browse files Browse the repository at this point in the history
…er a concatenation (plus sign).

This code makes the new assumption that a `'` that follows `+` must be a string.

This change will break YANG files where there exists an unquoted string that contains a `+'` character substring. Given that this is unlikely, I think this is a fine tradeoff to get cases like #180 working.

Indeed, the current lexer is already assuming `"` cannot exist within an unquoted string (see `lexIdentifier`). This is already a much more restrictive assumption.

NOTE: In YANG1.1:
```
An unquoted string cannot contain any single or double quote
      characters.
```

Fixes #180.
  • Loading branch information
wenovus committed Jun 18, 2021
1 parent 910fedf commit e9586e9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
12 changes: 11 additions & 1 deletion pkg/yang/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,18 @@ func lexGround(l *lexer) stateFn {
l.next()
l.next()
return lexGround
default:
return lexIdentifier
}
case '+':
l.next()
switch l.peek() {
case '"', '\'':
l.emit(tIdentifier)
return lexGround
default:
return lexIdentifier
}
fallthrough
default:
return lexIdentifier
}
Expand Down
13 changes: 12 additions & 1 deletion pkg/yang/lex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ Tests:
T(tIdentifier, "fred"),
}},
{line(), `
pattern '[a-zA-Z0-9!#$%&'+"'"+'*+/=?^_` + "`" + `{|}~-]+';
`, []*token{
T(tIdentifier, "pattern"),
T(tString, "[a-zA-Z0-9!#$%&"),
T(tIdentifier, "+"),
T(tString, "'"),
T(tIdentifier, "+"),
T(tString, "*+/=?^_`{|}~-]+"),
T(';', ";"),
}},
{line(), `
// tab indent both lines
"Broken
line"
Expand Down Expand Up @@ -182,7 +193,7 @@ Tests:
continue Tests
}
if len(tt.tokens) > i && !token.Equal(tt.tokens[i]) {
t.Errorf("%d: got %v want %v", tt.line, token, tt.tokens[i])
t.Errorf("%d, %d: got (%v, %q) want (%v, %q)", tt.line, i, token.code, token.Text, tt.tokens[i].code, tt.tokens[i].Text)
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/yang/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,27 @@ foo1 {
},
},
{line: line(), in: `
foo1 {
key value1;
foo2 {
pattern '[a-zA-Z0-9!#$%&'+"'"+'*+/=?^_` + "`" + `{|}~-]+'
+ '(\.[a-zA-Z0-9!#$%&'+"'"+'*+/=?^_` + "`" + `{|}~-]+)*'
+ '@'
+ '[a-zA-Z0-9!#$%&'+"'"+'*+/=?^_` + "`" + `{|}~-]+'
+ '(\.[a-zA-Z0-9!#$%&'+"'"+'*+/=?^_` + "`" + `{|}~-]+)*';
}
}
`,
out: []*Statement{
S("foo1",
SA("key", "value1"),
S("foo2",
SA("pattern", "[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(\\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(\\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*"),
),
),
},
},
{line: line(), in: `
}
`,
err: `test.yang:2:2: unexpected }`,
Expand Down

0 comments on commit e9586e9

Please sign in to comment.