Skip to content

Commit

Permalink
Handle comments between concatenated string literals (#940)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriehSchneier authored Aug 29, 2024
1 parent f6f3f79 commit f69d498
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
Binary file modified internal/bundles/assets/import_proto_cli.arraiz
Binary file not shown.
17 changes: 10 additions & 7 deletions pkg/importer/proto/proto_parser.arrai
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ floatLit -> /{\d+(?:\.\d+?)? (?:e[-+]?\d+)? | \. \d+ (?:e[-+]?\d+)? | inf

boolLit -> /{true | false};

strLit -> ('"' s=([^"]*:'""') '"' | "'" s=([^']*:"''") "'"):\s; //';
strLit -> (c* (s=(["]\s* [^"]*:'""') ["] | s=([']\s* [^']*:"''") ['])):\s; // ";

object -> "{" (c* objectField comma? c*)* "}";
objectField -> objectKey ":"? objectValue;
Expand Down Expand Up @@ -137,12 +137,15 @@ let concatStrLit = \strLit
//seq.concat(//seq.concat(
(strLit.s >> .'') => \i (
@:i.@,
@item: i.@item >> \v
let s=(v rank (:.@));
cond strLit.@choice(i.@) {
0: cond s {'""':'"', _:s},
1: cond s {"''":"'", _:s},
}
@item: i.@item => \v
let st=(v.@item rank (:.@));
let s=cond {v.@=0:st where .@>0 rank (:.@), _:st};
(@:v.@, @item:
cond strLit.@choice(i.@) {
0: cond s {'""':'"', _:s},
1: cond s {"''":"'", _:s},
}
)
)
));

Expand Down
20 changes: 16 additions & 4 deletions pkg/importer/proto/proto_parser_test.arrai
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ let (:evalConstant, :parseFile, ...) = //{./proto_parser};
(
constant: (
string:
let constant = (strLit: ('': [153\'"', 231\'"'], @choice: [0], s: [('': [154\'test'])]));
let constant = (strLit: ('': [231\'"'], @choice: [0], s: [('': [153\'"', 154\'test'])]));
let expected = (s: "test");
let actual = evalConstant(constant);
//test.assert.equal(expected, actual),
Expand All @@ -23,12 +23,12 @@ let (:evalConstant, :parseFile, ...) = //{./proto_parser};
let actual = evalConstant(constant);
//test.assert.equal(expected, actual),
object:
let constant = (object: (objectField: [('': 1390\':', objectKey: (constant: (fullIdent: (ident: [('': [1387\'g', 1388\'et'])]))), objectValue: (constant: (strLit: ('': [1392\'"', 1415\'"'], @choice: [0], s: [('': [1393\'/api/accounts'])]))))]));
let constant = (object: (objectField: [('': 1390\':', objectKey: (constant: (fullIdent: (ident: [('': [1387\'g', 1388\'et'])]))), objectValue: (constant: (strLit: ('': [1415\'"'], @choice: [0], s: [('': [1392\'"', 1393\'/api/accounts'])]))))]));
let expected = (fields: {"get": (s: "/api/accounts")});
let actual = evalConstant(constant);
//test.assert.equal(expected, actual),
array:
let constant = (array: ('': [1497\'[', 1543\']'], arrayItem: [(constant: (strLit: ('': [1498\'"', 1542\'"'], @choice: [0], s: [('': [1499\'string'])]))), (constant: (intLit: (decimalLit: ('': 235\'2'))))]));
let constant = (array: ('': [1497\'[', 1543\']'], arrayItem: [(constant: (strLit: ('': [1542\'"'], @choice: [0], s: [('': [1498\'"', 1499\'string'])]))), (constant: (intLit: (decimalLit: ('': 235\'2'))))]));
let expected = [(s: "string"), (d: 2)];
let actual = evalConstant(constant);
//test.assert.equal(expected, actual),
Expand Down Expand Up @@ -87,10 +87,22 @@ let (:evalConstant, :parseFile, ...) = //{./proto_parser};
// Comment
]
// Comment
foo2:
// Comment
"two"
// Comment
" halves"
// Comment
foo3:
// Comment
'two'
// Comment
' halves'
// Comment
};
// Comment
`);
let expected = {'(foo.java_package).bar': (fields: {'foo': [(s: 'bar')]})};
let expected = {'(foo.java_package).bar': (fields: {'foo': [(s: 'bar')], 'foo2': (s: 'two halves'), 'foo3': (s: "two halves")})};
let actual = file.options;
//test.assert.equal(expected, actual),
),
Expand Down

0 comments on commit f69d498

Please sign in to comment.