Skip to content

Commit b2a5e6a

Browse files
committed
Fix bug in kv parser
1 parent c36c02a commit b2a5e6a

File tree

4 files changed

+62
-27
lines changed

4 files changed

+62
-27
lines changed

packages/kv/src/parser-types.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,4 @@ export class Document {
389389
return this.errors.length > 0;
390390
}
391391

392-
public toStringPreservePositions(): string {
393-
394-
return "";
395-
}
396-
397392
}

packages/kv/src/parser.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,35 @@ describe("Parse Successes", () => {
156156

157157
});
158158

159+
test("Parse compile time report", () => {
160+
const kvFile =
161+
`"report"
162+
{
163+
"total_seconds" "12"
164+
}
165+
`;
166+
const kvTree = parser.parseText(kvFile);
167+
expect(kvTree.getErrors().length).toBe(0);
168+
expect(kvTree.getRootItems().length).toBe(1);
169+
170+
const root = kvTree.getRootItems()[0];
171+
expect(root.isRoot()).toBeTruthy();
172+
expect(root.isLeaf()).toBeFalsy();
173+
expect(root.getKey().getContent()).toBe("\"report\"");
174+
175+
const children = root.getChildren()!;
176+
expect(children).not.toBeNull();
177+
expect(children.length).toBe(1);
178+
const item1 = children[0];
179+
expect(item1.isLeaf()).toBeTruthy();
180+
expect(item1.isRoot()).toBeFalsy();
181+
expect(item1.getKey().getContent()).toBe("\"total_seconds\"");
182+
expect(item1.getValues()).not.toBeNull();
183+
expect(item1.getValues()!.length).toBe(1);
184+
expect(item1.getValues()![0].getContent()).toBe("\"12\"");
185+
});
186+
187+
159188
test("With Conditional", () => {
160189

161190
const kvFile =

packages/kv/src/parser.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -123,28 +123,29 @@ function getKvSetPosition(s: ParserState): Position | null {
123123
}
124124

125125
function completeOutstandingKvSet(s: ParserState, token: Token): void {
126-
if(s.keyToken != null && s.keyToken.line !== token.line) {
127-
if(s.valueTokens.length == 0) {
128-
129-
const error = new ParseError(ParseErrorType.MissingValue, s.keyToken.getPosition());
130-
s.errors.push(error);
131-
}
126+
if(s.keyToken == null || s.keyToken.line === token.line || token.type === TokenType.ObjectStart) return;
132127

133-
if(s.currentParent == null) {
134-
const pos = getKvSetPosition(s);
135-
if(pos != null) {
136-
s.errors.push(new ParseError(ParseErrorType.MissingRootObject, pos));
137-
}
138-
} else {
139-
const key = s.keyToken.toLiteral();
140-
const values = s.valueTokens.map(t => new Literal(new Position(t.line, t.range), t.value));
141-
const condition: Conditional | undefined = s.conditionToken?.toConditional();
142-
const item = Item.createLeaf(s.currentParent, key, values, condition);
143-
s.currentParent.addChild(item);
144-
}
128+
if(s.valueTokens.length == 0) {
129+
130+
const error = new ParseError(ParseErrorType.MissingValue, s.keyToken.getPosition());
131+
s.errors.push(error);
132+
}
145133

146-
s.keyToken = null;
147-
s.valueTokens = [];
148-
s.conditionToken = null;
134+
if(s.currentParent == null) {
135+
const pos = getKvSetPosition(s);
136+
if(pos != null) {
137+
s.errors.push(new ParseError(ParseErrorType.MissingRootObject, pos));
138+
}
139+
} else {
140+
const key = s.keyToken.toLiteral();
141+
const values = s.valueTokens.map(t => new Literal(new Position(t.line, t.range), t.value));
142+
const condition: Conditional | undefined = s.conditionToken?.toConditional();
143+
const item = Item.createLeaf(s.currentParent, key, values, condition);
144+
s.currentParent.addChild(item);
149145
}
146+
147+
s.keyToken = null;
148+
s.valueTokens = [];
149+
s.conditionToken = null;
150+
150151
}

packages/kv/src/tokenizer.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,4 +246,14 @@ test("Error when / is not followed by any character", () => {
246246
}).not.toThrowError();
247247

248248

249-
});
249+
});
250+
251+
test("Tokenize compile time report", () => {
252+
const kvFile =
253+
`"report"
254+
{
255+
"total_seconds" "12"
256+
}
257+
`;
258+
const tokens = tokenize(kvFile);
259+
});

0 commit comments

Comments
 (0)