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

Commit fd65240

Browse files
committed
include token ids in release header
1 parent a454f04 commit fd65240

File tree

8 files changed

+116
-60
lines changed

8 files changed

+116
-60
lines changed

.depend

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
alloc.o: alloc.c alloc.h
33

44
api.o: api.c api.h parser_options.h string.h decoder.h bytes.h \
5-
token_rewriter.h token.h loc.h shared_byte_list.h parser_result.h \
6-
source_line.h nodes.h diagnostic.h messages.h comment.h \
7-
magic_comment.h test_helper.h
5+
token_rewriter.h token.h loc.h token_ids.h shared_byte_list.h \
6+
parser_result.h source_line.h nodes.h diagnostic.h messages.h \
7+
comment.h magic_comment.h test_helper.h
88

99
bytes.o: bytes.c bytes.h test_helper.h
1010

@@ -24,12 +24,12 @@ messages.o: messages.c messages.h string.h test_helper.h
2424
nodes.o: nodes.c nodes.h string.h loc.h bytes.h test_helper.h
2525

2626
parser_options.o: parser_options.c parser_options.h string.h decoder.h \
27-
bytes.h token_rewriter.h token.h loc.h shared_byte_list.h \
27+
bytes.h token_rewriter.h token.h loc.h token_ids.h shared_byte_list.h \
2828
test_helper.h
2929

3030
parser_result.o: parser_result.c parser_result.h string.h source_line.h \
31-
bytes.h nodes.h loc.h token.h diagnostic.h messages.h comment.h \
32-
magic_comment.h test_helper.h
31+
bytes.h nodes.h loc.h token.h token_ids.h diagnostic.h messages.h \
32+
comment.h magic_comment.h test_helper.h
3333

3434
shared_byte_list.o: shared_byte_list.c shared_byte_list.h test_helper.h
3535

@@ -39,17 +39,17 @@ string.o: string.c string.h test_helper.h
3939

4040
test_helper.o: test_helper.c test_helper.h string.h
4141

42-
token.o: token.c token.h bytes.h loc.h test_helper.h
42+
token.o: token.c token.h bytes.h loc.h token_ids.h test_helper.h
4343

4444
token_rewriter.o: token_rewriter.c token_rewriter.h token.h bytes.h loc.h \
45-
shared_byte_list.h test_helper.h
45+
token_ids.h shared_byte_list.h test_helper.h
4646

4747
tests/runner: tests/test.c tests/../test_helper.h tests/../bytes.h \
4848
tests/../comment.h tests/../loc.h tests/../decoder.h tests/../string.h \
4949
tests/../diagnostic.h tests/../messages.h tests/../magic_comment.h \
5050
tests/../nodes.h tests/../parser_options.h tests/../token_rewriter.h \
51-
tests/../token.h tests/../shared_byte_list.h tests/../parser_result.h \
52-
tests/../source_line.h tests/../api.h
51+
tests/../token.h tests/../token_ids.h tests/../shared_byte_list.h \
52+
tests/../parser_result.h tests/../source_line.h tests/../api.h
5353

5454
benchmark/c-parser: benchmark/benchmark.c benchmark/../lib-ruby-parser.h \
5555
benchmark/rb_filelist.h

codegen/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ version = "0.1.0"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

8-
[build-dependencies]
8+
[dependencies]
99
cbindgen = "0.20.0"
10+
11+
[build-dependencies]
1012
lib-ruby-parser-nodes = {version = "0.38.0"}

codegen/build.mk

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,19 @@ ruby-parser-c/src/message.rs: $(CODEGEN_DEPS)
3838
$(DO_CODEGEN)
3939
CLEAN += ruby-parser-c/src/message.rs
4040

41-
lib-ruby-parser.h: codegen/examples/merge_headers.rs $(H_FILES)
41+
# token_id.{h,c} codegen
42+
DO_CODEGEN_TOKEN_IDS = cargo run --example build_token_ids --manifest-path codegen/Cargo.toml
43+
44+
token_ids.h: codegen/examples/build_token_ids.rs
45+
$(DO_CODEGEN_TOKEN_IDS)
46+
CLEAN += token_ids.h
47+
update-depend: token_ids.h
48+
49+
# lib-ruby-parser.h codegen
50+
lib-ruby-parser.h: codegen/examples/merge_headers.rs $(H_FILES) token_ids.h
4251
cargo run --example merge_headers --manifest-path codegen/Cargo.toml
4352
CLEAN += lib-ruby-parser.h
53+
update-depend: lib-ruby-parser.h
4454

4555
# manual codegen task
4656
do-codegen:

codegen/build.rs

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,7 @@
1-
extern crate cbindgen;
21
extern crate lib_ruby_parser_nodes;
32

43
mod codegen;
54

65
fn main() {
76
codegen::codegen();
8-
9-
tokend_ids::codegen();
10-
}
11-
12-
mod tokend_ids {
13-
const TOKEN_IDS: &str = "../token_ids.h";
14-
15-
pub(crate) fn codegen() {
16-
cbindgen::Builder::new()
17-
.with_crate("../ruby-parser-c")
18-
.with_parse_deps(true)
19-
.with_parse_extra_bindings(&["lib-ruby-parser"])
20-
.with_no_includes()
21-
.include_item("Lexer")
22-
.generate()
23-
.expect("Unable to generate bindings")
24-
.write_to_file(TOKEN_IDS);
25-
26-
let contents = std::fs::read_to_string(TOKEN_IDS).unwrap();
27-
let mut token_ids = vec![];
28-
for line in contents.lines() {
29-
if line.starts_with("/// Token") {
30-
// token comment line
31-
token_ids.push(line.to_string());
32-
} else if line.contains("const int32_t Lexer_") {
33-
let line = line
34-
.replace("static ", "")
35-
.replace("Lexer_", "LIB_RUBY_PARSER_TOKEN_");
36-
token_ids.push(line);
37-
token_ids.push(String::from(""))
38-
}
39-
}
40-
41-
let contents = format!(
42-
"#ifndef LIB_RUBY_PARSER_TOKEN_IDS_H
43-
#define LIB_RUBY_PARSER_TOKEN_IDS_H
44-
45-
#include <stdint.h>
46-
47-
{token_ids}
48-
#endif // LIB_RUBY_PARSER_TOKEN_IDS_H",
49-
token_ids = token_ids.join("\n")
50-
);
51-
52-
std::fs::write(TOKEN_IDS, contents).unwrap();
53-
}
547
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
extern crate cbindgen;
2+
3+
#[derive(Debug, Default)]
4+
struct TokenId {
5+
comment: String,
6+
name: String,
7+
value: String,
8+
}
9+
10+
impl TokenId {
11+
fn declaration(&self) -> String {
12+
format!(
13+
"{comment}
14+
#define LIB_RUBY_PARSER_TOKEN_{name} {value}",
15+
comment = self.comment,
16+
name = self.name,
17+
value = self.value
18+
)
19+
}
20+
}
21+
22+
fn bindgen() -> String {
23+
const TMP_BINDINGS: &str = "codegen/examples/target/tmp_bindings.h";
24+
25+
cbindgen::Builder::new()
26+
.with_crate("ruby-parser-c")
27+
.with_parse_deps(true)
28+
.with_parse_extra_bindings(&["lib-ruby-parser"])
29+
.with_no_includes()
30+
.include_item("Lexer")
31+
.generate()
32+
.expect("Unable to generate bindings")
33+
.write_to_file(TMP_BINDINGS);
34+
35+
let contents = std::fs::read_to_string(TMP_BINDINGS).unwrap();
36+
37+
std::fs::remove_file(TMP_BINDINGS).unwrap();
38+
39+
contents
40+
}
41+
42+
fn collect_token_ids() -> Vec<TokenId> {
43+
let bindings = bindgen();
44+
45+
let mut token_ids = vec![];
46+
let mut token_id = TokenId::default();
47+
48+
for line in bindings.lines() {
49+
if line.starts_with("/// Token") {
50+
// token comment line
51+
token_id.comment = line.to_string();
52+
} else if line.starts_with("static const int32_t Lexer_") {
53+
let line = line
54+
.replace("static const int32_t Lexer_", "")
55+
.replace(";", "");
56+
57+
let parts = line.split(" = ").collect::<Vec<_>>();
58+
assert_eq!(parts.len(), 2);
59+
token_id.name = parts[0].to_string();
60+
token_id.value = parts[1].to_string();
61+
62+
token_ids.push(std::mem::take(&mut token_id));
63+
}
64+
}
65+
66+
token_ids
67+
}
68+
69+
pub fn main() {
70+
let token_ids = collect_token_ids();
71+
72+
let token_ids_h = format!(
73+
"#ifndef LIB_RUBY_PARSER_TOKEN_IDS_H
74+
#define LIB_RUBY_PARSER_TOKEN_IDS_H
75+
76+
{declarations}
77+
78+
#endif // LIB_RUBY_PARSER_TOKEN_IDS_H
79+
",
80+
declarations = token_ids
81+
.iter()
82+
.map(TokenId::declaration)
83+
.collect::<Vec<_>>()
84+
.join("\n\n")
85+
);
86+
87+
std::fs::write("token_ids.h", token_ids_h).unwrap();
88+
}

codegen/examples/merge_headers.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const HEADERS: &[&str] = &[
44
"bytes.h",
55
"source_line.h",
66
"loc.h",
7+
"token_ids.h",
78
"token.h",
89
"comment.h",
910
"magic_comment.h",
@@ -49,7 +50,7 @@ fn process_header(header_path: &str, sys_includes: &mut Vec<String>, contents: &
4950
continue;
5051
}
5152
if line.starts_with("#ifndef LIB_RUBY_PARSER_")
52-
|| line.starts_with("#define LIB_RUBY_PARSER_")
53+
|| (line.starts_with("#define LIB_RUBY_PARSER_") && line.ends_with("_H"))
5354
|| line.starts_with("#endif // LIB_RUBY_PARSER_")
5455
{
5556
// header guard, skip

token.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ static void test_token_fields(void)
1313

1414
LIB_RUBY_PARSER_Token token = lib_ruby_parser__test__make_token_eq((LIB_RUBY_PARSER_Loc){.begin = 1, .end = 2}, 3, 4);
1515

16+
assert_eq(token.token_type, LIB_RUBY_PARSER_TOKEN_tEQ);
1617
assert_token(token, "tEQ");
1718
assert_byte_list(token.token_value.raw, "=");
1819
assert_loc(token.loc, 1, 2);

token.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "bytes.h"
55
#include "loc.h"
6+
#include "token_ids.h"
67

78
/*
89
Equivalent of `lib_ruby_parser::Token`

0 commit comments

Comments
 (0)