Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit cbf66f6

Browse files
committed
pass record_tokens/debug/buffer_name via parser options
1 parent 88a52d2 commit cbf66f6

File tree

2 files changed

+58
-27
lines changed

2 files changed

+58
-27
lines changed

node_bindings.cc

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,50 @@ namespace lib_ruby_parser_node
1212
{
1313
Env env = info.Env();
1414

15-
if (info.Length() != 1)
15+
if (info.Length() != 2)
1616
{
17-
TypeError::New(env, "Wrong number of arguments")
17+
TypeError::New(env, "Wrong number of arguments (expected 2)")
1818
.ThrowAsJavaScriptException();
1919
return env.Null();
2020
}
2121

22-
Value arg = info[0];
23-
if (!arg.IsString())
22+
Value source_arg = info[0];
23+
if (!source_arg.IsString())
2424
{
25-
TypeError::New(env, "Argument must be a string")
25+
TypeError::New(env, "The first argument must be a string")
2626
.ThrowAsJavaScriptException();
2727
return env.Null();
2828
}
29+
std::string source = source_arg.As<String>().Utf8Value();
30+
31+
Value options_arg = info[1];
32+
if (!options_arg.IsObject())
33+
{
34+
TypeError::New(env, "The second argument must be an object")
35+
.ThrowAsJavaScriptException();
36+
return env.Null();
37+
}
38+
Object options_obj = options_arg.As<Object>();
2939

30-
std::string source = arg.As<String>().Utf8Value();
3140
ParserOptions options;
32-
options.record_tokens = true;
41+
options.record_tokens = options_obj.Get("record_tokens").ToBoolean().Value();
42+
options.debug = options_obj.Get("debug").ToBoolean().Value();
43+
auto buffer_name = options_obj.Get("buffer_name");
44+
if (buffer_name.IsString())
45+
{
46+
options.buffer_name = buffer_name.As<String>().Utf8Value();
47+
}
48+
else if (buffer_name.IsUndefined())
49+
{
50+
// ok, default is used
51+
}
52+
else
53+
{
54+
TypeError::New(env, "buffer_name must be string/undefined")
55+
.ThrowAsJavaScriptException();
56+
return env.Null();
57+
}
58+
3359
auto result = ParserResult::from_source(source, std::move(options));
3460
return convert(std::move(result), env);
3561
}

test.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,30 @@ function assert_node_type(node, type) {
2828
}
2929

3030
function assert_range(range, begin_pos, end_pos) {
31-
assert(range instanceof Range, `can't assert begin_pos/end_pos of null range`)
31+
assert(range instanceof Range, `expected ${range} to be an instance of Range`)
3232
assert(range.begin_pos === begin_pos, `expected 'begin_pos' of range to be ${begin_pos}, got ${range.begin_pos}`)
3333
assert(range.end_pos === end_pos, `expected 'end_pos' of range to be ${end_pos}, got ${range.end_pos}`)
3434
}
3535

36+
function assert_token(token, name, value, loc) {
37+
assert(token instanceof Token, `expected ${token} to be an instance of Token`)
38+
assert(token.name == name, `expected token's name to be ${name}, got ${token.name}`)
39+
assert(token.value == value, `expected token's value to be ${value}, got ${token.value}`)
40+
assert(token.loc.begin == loc.begin, `expected token's begin to be ${loc.begin}, got ${token.loc.begin}`)
41+
assert(token.loc.end == loc.end, `expected token's end to be ${loc.end}, got ${token.loc.end}`)
42+
}
43+
3644
assert_throws(42)
3745
assert_throws("foo", 10)
3846

39-
let result = parse("self.foo(123)")
47+
let result = parse(
48+
"self.foo(123)",
49+
{
50+
record_tokens: true,
51+
debug: true,
52+
buffer_name: "(test)"
53+
}
54+
)
4055
console.log(inspect(result, { showHidden: false, depth: null }));
4156

4257
assert(result !== null, "expected result to be non-null")
@@ -66,24 +81,14 @@ assert(arg.value === "123")
6681
assert(arg.operator_l === null)
6782
assert_range(arg.expression_l, 9, 12)
6883

69-
let expected_tokens = [
70-
new Token("kSELF", "self", new Loc(0, 4)),
71-
new Token("tDOT", ".", new Loc(4, 5)),
72-
new Token("tIDENTIFIER", "foo", new Loc(5, 8)),
73-
new Token("tLPAREN2", "(", new Loc(8, 9)),
74-
new Token("tINTEGER", "123", new Loc(9, 12)),
75-
new Token("tRPAREN", ")", new Loc(12, 13)),
76-
new Token("EOF", "", new Loc(13, 13)),
77-
]
78-
79-
assert(tokens.length == expected_tokens.length)
80-
81-
for (let i = 0; i < tokens.length; i++) {
82-
assert(tokens[i].name == expected_tokens[i].name)
83-
assert(tokens[i].value == expected_tokens[i].value)
84-
assert(tokens[i].loc.begin == expected_tokens[i].loc.begin)
85-
assert(tokens[i].loc.end == expected_tokens[i].loc.end)
86-
}
84+
assert(tokens.length == 7)
85+
assert_token(tokens[0], "kSELF", "self", new Loc(0, 4))
86+
assert_token(tokens[1], "tDOT", ".", new Loc(4, 5))
87+
assert_token(tokens[2], "tIDENTIFIER", "foo", new Loc(5, 8))
88+
assert_token(tokens[3], "tLPAREN2", "(", new Loc(8, 9))
89+
assert_token(tokens[4], "tINTEGER", "123", new Loc(9, 12))
90+
assert_token(tokens[5], "tRPAREN", ")", new Loc(12, 13))
91+
assert_token(tokens[6], "EOF", "", new Loc(13, 13))
8792

8893
assert(diagnostics.length === 0)
8994
assert(comments.length === 0)

0 commit comments

Comments
 (0)