Skip to content

Commit 1580609

Browse files
authored
Merge pull request #181 from openconfig/fix-adjacent-string-concat
Fix lexer to identify single-quote string that immediately occurs after a concatenation (plus sign).
2 parents d2f2363 + e9586e9 commit 1580609

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

pkg/yang/lex.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,18 @@ func lexGround(l *lexer) stateFn {
386386
l.next()
387387
l.next()
388388
return lexGround
389+
default:
390+
return lexIdentifier
391+
}
392+
case '+':
393+
l.next()
394+
switch l.peek() {
395+
case '"', '\'':
396+
l.emit(tIdentifier)
397+
return lexGround
398+
default:
399+
return lexIdentifier
389400
}
390-
fallthrough
391401
default:
392402
return lexIdentifier
393403
}

pkg/yang/lex_test.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,17 @@ Tests:
122122
T(tIdentifier, "fred"),
123123
}},
124124
{line(), `
125+
pattern '[a-zA-Z0-9!#$%&'+"'"+'*+/=?^_` + "`" + `{|}~-]+';
126+
`, []*token{
127+
T(tIdentifier, "pattern"),
128+
T(tString, "[a-zA-Z0-9!#$%&"),
129+
T(tIdentifier, "+"),
130+
T(tString, "'"),
131+
T(tIdentifier, "+"),
132+
T(tString, "*+/=?^_`{|}~-]+"),
133+
T(';', ";"),
134+
}},
135+
{line(), `
125136
// tab indent both lines
126137
"Broken
127138
line"
@@ -182,7 +193,7 @@ Tests:
182193
continue Tests
183194
}
184195
if len(tt.tokens) > i && !token.Equal(tt.tokens[i]) {
185-
t.Errorf("%d: got %v want %v", tt.line, token, tt.tokens[i])
196+
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)
186197
}
187198
}
188199
}

pkg/yang/parse_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,27 @@ foo1 {
217217
},
218218
},
219219
{line: line(), in: `
220+
foo1 {
221+
key value1;
222+
foo2 {
223+
pattern '[a-zA-Z0-9!#$%&'+"'"+'*+/=?^_` + "`" + `{|}~-]+'
224+
+ '(\.[a-zA-Z0-9!#$%&'+"'"+'*+/=?^_` + "`" + `{|}~-]+)*'
225+
+ '@'
226+
+ '[a-zA-Z0-9!#$%&'+"'"+'*+/=?^_` + "`" + `{|}~-]+'
227+
+ '(\.[a-zA-Z0-9!#$%&'+"'"+'*+/=?^_` + "`" + `{|}~-]+)*';
228+
}
229+
}
230+
`,
231+
out: []*Statement{
232+
S("foo1",
233+
SA("key", "value1"),
234+
S("foo2",
235+
SA("pattern", "[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(\\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(\\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*"),
236+
),
237+
),
238+
},
239+
},
240+
{line: line(), in: `
220241
}
221242
`,
222243
err: `test.yang:2:2: unexpected }`,

0 commit comments

Comments
 (0)