Skip to content

Commit

Permalink
Don't swallow exceptions in Parser.run (#111)
Browse files Browse the repository at this point in the history
Also, this adds a few fixtures for testing the missing final newline.
  • Loading branch information
stasm authored May 18, 2018
1 parent 61a3fe0 commit 73170f0
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 9 deletions.
9 changes: 2 additions & 7 deletions lib/parser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,10 @@ export default class Parser {
}

run(iterable) {
const stream = iterable instanceof Stream
let stream = iterable instanceof Stream
? iterable
: new Stream(iterable);
try {
return this.parse(stream);
} catch (err) {
// console.error(err.message);
return new Failure(err.message, stream);
}
return this.parse(stream);
}

get abstract() {
Expand Down
4 changes: 2 additions & 2 deletions lib/stream.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export default class Stream {
: length;
}

// Get the first value from the iterable.
// Get the element at the cursor.
head() {
if (this.length < 0) {
throw new TypeError("index out of range");
return undefined;
}

if (this.length === 0) {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/eof_comment.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### NOTE: Disable final newline insertion when editing this file.

# No EOL
15 changes: 15 additions & 0 deletions test/fixtures/eof_comment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type": "Resource",
"body": [
{
"type": "ResourceComment",
"annotations": [],
"content": "NOTE: Disable final newline insertion when editing this file.\n"
},
{
"type": "Comment",
"annotations": [],
"content": "No EOL"
}
]
}
Empty file added test/fixtures/eof_empty.ftl
Empty file.
4 changes: 4 additions & 0 deletions test/fixtures/eof_empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "Resource",
"body": []
}
3 changes: 3 additions & 0 deletions test/fixtures/eof_id.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### NOTE: Disable final newline insertion when editing this file.

message-id
15 changes: 15 additions & 0 deletions test/fixtures/eof_id.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type": "Resource",
"body": [
{
"type": "ResourceComment",
"annotations": [],
"content": "NOTE: Disable final newline insertion when editing this file.\n"
},
{
"type": "Junk",
"annotations": [],
"content": "message-id"
}
]
}
3 changes: 3 additions & 0 deletions test/fixtures/eof_id_equals.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### NOTE: Disable final newline insertion when editing this file.

message-id =
15 changes: 15 additions & 0 deletions test/fixtures/eof_id_equals.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type": "Resource",
"body": [
{
"type": "ResourceComment",
"annotations": [],
"content": "NOTE: Disable final newline insertion when editing this file.\n"
},
{
"type": "Junk",
"annotations": [],
"content": "message-id ="
}
]
}
3 changes: 3 additions & 0 deletions test/fixtures/eof_junk.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### NOTE: Disable final newline insertion when editing this file.

000
15 changes: 15 additions & 0 deletions test/fixtures/eof_junk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type": "Resource",
"body": [
{
"type": "ResourceComment",
"annotations": [],
"content": "NOTE: Disable final newline insertion when editing this file.\n"
},
{
"type": "Junk",
"annotations": [],
"content": "000"
}
]
}
3 changes: 3 additions & 0 deletions test/fixtures/eof_value.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### NOTE: Disable final newline insertion when editing this file.

no-eol = No EOL
29 changes: 29 additions & 0 deletions test/fixtures/eof_value.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"type": "Resource",
"body": [
{
"type": "ResourceComment",
"annotations": [],
"content": "NOTE: Disable final newline insertion when editing this file.\n"
},
{
"type": "Message",
"annotations": [],
"id": {
"type": "Identifier",
"name": "no-eol"
},
"value": {
"type": "Pattern",
"elements": [
{
"type": "TextElement",
"value": "No EOL"
}
]
},
"attributes": [],
"comment": null
}
]
}

0 comments on commit 73170f0

Please sign in to comment.