Skip to content

Commit cbd6799

Browse files
committed
lexer tests: makefile/configure
1 parent 188d889 commit cbd6799

File tree

5 files changed

+84
-2
lines changed

5 files changed

+84
-2
lines changed

Makefile.in

+13
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,19 @@ ifneq ($(findstring clean,$(MAKECMDGOALS)),)
252252
include $(CFG_SRC_DIR)mk/clean.mk
253253
endif
254254

255+
# Grammar tests
256+
257+
ifneq ($(findstring lexer,$(MAKECMDGOALS)),)
258+
ifdef CFG_JAVAC
259+
ifdef CFG_ANTLR4
260+
ifdef CFG_GRUN
261+
CFG_INFO := $(info cfg: including grammar tests)
262+
include $(CFG_SRC_DIR)mk/grammar.mk
263+
endif
264+
endif
265+
endif
266+
endif
267+
255268
# CTAGS building
256269
ifneq ($(strip $(findstring TAGS.emacs,$(MAKECMDGOALS)) \
257270
$(findstring TAGS.vi,$(MAKECMDGOALS))),)

configure

+3
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,9 @@ probe CFG_VALGRIND valgrind
493493
probe CFG_PERF perf
494494
probe CFG_ISCC iscc
495495
probe CFG_LLNEXTGEN LLnextgen
496+
probe CFG_JAVAC javac
497+
probe CFG_ANTLR4 antlr4
498+
probe CFG_GRUN grun
496499
probe CFG_PANDOC pandoc
497500
probe CFG_PDFLATEX pdflatex
498501
probe CFG_XELATEX xelatex

mk/grammar.mk

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
# file at the top-level directory of this distribution and at
3+
# http://rust-lang.org/COPYRIGHT.
4+
#
5+
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
# option. This file may not be copied, modified, or distributed
9+
# except according to those terms.
10+
11+
BG = $(CFG_BUILD_DIR)/grammar/
12+
SG = $(S)src/grammar/
13+
B = $(CFG_BUILD_DIR)/$(CFG_BUILD)/stage2/
14+
L = $(B)lib/rustlib/$(CFG_BUILD)/lib
15+
LD = $(CFG_BUILD)/stage2/lib/rustlib/$(CFG_BUILD)/lib/
16+
RUSTC = $(B)bin/rustc
17+
18+
# Run the reference lexer against libsyntax and compare the tokens and spans.
19+
# If "// ignore-lexer-test" is present in the file, it will be ignored.
20+
#
21+
# $(1) is the file to test.
22+
define LEXER_TEST
23+
grep "// ignore-lexer-test" $(1) ; \
24+
if [ $$? -eq 1 ]; then \
25+
CLASSPATH=$(B)grammar $(CFG_GRUN) RustLexer tokens -tokens < $(1) \
26+
| $(B)grammar/verify $(1) ; \
27+
fi
28+
endef
29+
30+
$(BG):
31+
$(Q)mkdir -p $(BG)
32+
33+
$(BG)RustLexer.class: $(SG)RustLexer.g4
34+
$(Q)$(CFG_ANTLR4) -o $(B)grammar $(SG)RustLexer.g4
35+
$(Q)$(CFG_JAVAC) -d $(BG) $(BG)RustLexer.java
36+
37+
$(BG)verify: $(SG)verify.rs rustc-stage2-H-$(CFG_BUILD) $(LD)stamp.regex_macros $(LD)stamp.rustc
38+
$(Q)$(RUSTC) -O --out-dir $(BG) -L $(L) $(SG)verify.rs
39+
40+
check-lexer: $(BG) $(BG)RustLexer.class $(BG)verify
41+
$(info Verifying libsyntax against the reference lexer ...)
42+
$(Q)find $(S) -iname '*.rs' -exec "$(SG)check.sh" {} "$(BG)" \
43+
"$(CFG_GRUN)" "$(BG)verify" "$(BG)RustLexer.tokens" "$(VERBOSE)" \;

src/grammar/check.sh

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/sh
2+
3+
# Run the reference lexer against libsyntax and compare the tokens and spans.
4+
# If "// ignore-lexer-test" is present in the file, it will be ignored.
5+
#
6+
# Argument $1 is the file to check, $2 is the classpath to use, $3 is the path
7+
# to the grun binary, $4 is the path to the verify binary, $5 is the path to
8+
# RustLexer.tokens
9+
10+
if [ "${VERBOSE}" == "1" ]; then
11+
set -x
12+
fi
13+
14+
grep -q "// ignore lexer-test" $1;
15+
16+
if [ $? -eq 1 ]; then
17+
cd $2 # This `cd` is so java will pick up RustLexer.class. I couldn't
18+
# figure out how to wrangle the CLASSPATH, just adding build/grammr didn't
19+
# seem to have anny effect.
20+
$3 RustLexer tokens -tokens < $1 | $4 $1 $5
21+
fi

src/grammar/verify.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,13 @@ fn main() {
223223
r.next_token()
224224
}
225225

226-
let token_map = parse_token_list(File::open(&Path::new("RustLexer.tokens")).unwrap().read_to_string().unwrap().as_slice());
226+
let args = std::os::args();
227+
228+
let token_map = parse_token_list(File::open(&Path::new(args.get(2).as_slice())).unwrap().read_to_string().unwrap().as_slice());
227229
let mut stdin = std::io::stdin();
228230
let mut antlr_tokens = stdin.lines().map(|l| parse_antlr_token(l.unwrap().as_slice().trim(), &token_map));
229231

230-
let code = File::open(&Path::new(std::os::args().get(1).as_slice())).unwrap().read_to_string().unwrap();
232+
let code = File::open(&Path::new(args.get(1).as_slice())).unwrap().read_to_string().unwrap();
231233
let options = config::basic_options();
232234
let session = session::build_session(options, None,
233235
syntax::diagnostics::registry::Registry::new([]));

0 commit comments

Comments
 (0)