Skip to content

Commit 6dc0aec

Browse files
committed
Clean up issue softdevteam#323 by using the quote crate.
1 parent 1a3b306 commit 6dc0aec

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

lrlex/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ lrpar = { path = "../lrpar", version = "0.12" }
2828
regex = "1"
2929
regex-syntax = "0.6.27"
3030
num-traits = "0.2"
31+
quote = "1.0"
3132
serde = "1.0"
3233
try_from = "0.3"

lrlex/src/lib/ctbuilder.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use cfgrammar::{newlinecache::NewlineCache, Spanned};
2020
use lazy_static::lazy_static;
2121
use lrpar::{CTParserBuilder, Lexeme};
2222
use num_traits::{AsPrimitive, PrimInt, Unsigned};
23+
use quote::quote;
2324
use regex::Regex;
2425
use serde::Serialize;
2526
use try_from::TryFrom;
@@ -416,12 +417,13 @@ pub fn lexerdef() -> {lexerdef_type} {{
416417

417418
outs.push_str(" let start_states: Vec<StartState> = vec![");
418419
for ss in lexerdef.iter_start_states() {
420+
let state_name = &ss.name;
419421
write!(
420422
outs,
421423
"
422-
StartState::new({}, {:?}, {}, ::cfgrammar::Span::new({}, {})),",
424+
StartState::new({}, {}, {}, ::cfgrammar::Span::new({}, {})),",
423425
ss.id,
424-
ss.name,
426+
quote!(#state_name),
425427
ss.exclusive,
426428
ss.name_span.start(),
427429
ss.name_span.end()
@@ -438,7 +440,7 @@ pub fn lexerdef() -> {lexerdef_type} {{
438440
None => "None".to_owned(),
439441
};
440442
let n = match r.name {
441-
Some(ref n) => format!("Some({:?}.to_string())", n),
443+
Some(ref n) => format!("Some({}.to_string())", quote!(#n)),
442444
None => "None".to_owned(),
443445
};
444446
let target_state = match r.target_state {
@@ -450,15 +452,17 @@ pub fn lexerdef() -> {lexerdef_type} {{
450452
r.name_span.start(),
451453
r.name_span.end()
452454
);
455+
let regex = &r.re_str;
456+
let start_states = r.start_states.as_slice();
453457
write!(
454458
outs,
455459
"
456-
Rule::new({}, {}, {}, \"{}\".to_string(), {:?}.to_vec(), {}).unwrap(),",
460+
Rule::new({}, {}, {}, {}.to_string(), {}.to_vec(), {}).unwrap(),",
457461
tok_id,
458462
n,
459463
n_span,
460-
r.re_str.replace('\\', "\\\\").replace('"', "\\\""),
461-
r.start_states,
464+
quote!(#regex),
465+
quote!([#(#start_states),*]),
462466
target_state,
463467
)
464468
.ok();
@@ -652,10 +656,12 @@ pub fn ct_token_map<StorageT: Display>(
652656
// forces a recompile, this will change this value, causing anything which depends on this
653657
// build of lrlex to be recompiled too.
654658
let mut outs = String::new();
659+
let timestamp = env!("VERGEN_BUILD_TIMESTAMP");
655660
write!(
656661
outs,
657-
"// lrlex build time: {:?}\n\nmod {} {{\n",
658-
env!("VERGEN_BUILD_TIMESTAMP"),
662+
"// lrlex build time: {}\n\nmod {} {{\n",
663+
quote!(#timestamp)
664+
,
659665
mod_name
660666
)
661667
.ok();

lrpar/cttests/src/quoting.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Test NoAction using the calculator grammar
2+
yacckind: Original(YaccOriginalActionKind::NoAction)
3+
grammar: |
4+
%start S
5+
%%
6+
S: '\' | '"' | '<' | '+' '🦀' ;
7+
8+
lexer: |
9+
%%
10+
" '"'
11+
\< '<'
12+
\\ '\'
13+
\+ '+'
14+
🦀 '🦀'
15+
[\t ]+ ;

0 commit comments

Comments
 (0)