Skip to content

Commit

Permalink
Clean up issue softdevteam#323 by using the quote crate.
Browse files Browse the repository at this point in the history
  • Loading branch information
ratmice committed Oct 9, 2022
1 parent 1a3b306 commit d005710
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
1 change: 1 addition & 0 deletions lrlex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ lrpar = { path = "../lrpar", version = "0.12" }
regex = "1"
regex-syntax = "0.6.27"
num-traits = "0.2"
quote = "1.0"
serde = "1.0"
try_from = "0.3"
21 changes: 13 additions & 8 deletions lrlex/src/lib/ctbuilder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use cfgrammar::{newlinecache::NewlineCache, Spanned};
use lazy_static::lazy_static;
use lrpar::{CTParserBuilder, Lexeme};
use num_traits::{AsPrimitive, PrimInt, Unsigned};
use quote::quote;
use regex::Regex;
use serde::Serialize;
use try_from::TryFrom;
Expand Down Expand Up @@ -416,12 +417,13 @@ pub fn lexerdef() -> {lexerdef_type} {{

outs.push_str(" let start_states: Vec<StartState> = vec![");
for ss in lexerdef.iter_start_states() {
let state_name = &ss.name;
write!(
outs,
"
StartState::new({}, {:?}, {}, ::cfgrammar::Span::new({}, {})),",
StartState::new({}, {}, {}, ::cfgrammar::Span::new({}, {})),",
ss.id,
ss.name,
quote!(#state_name),
ss.exclusive,
ss.name_span.start(),
ss.name_span.end()
Expand All @@ -438,7 +440,7 @@ pub fn lexerdef() -> {lexerdef_type} {{
None => "None".to_owned(),
};
let n = match r.name {
Some(ref n) => format!("Some({:?}.to_string())", n),
Some(ref n) => format!("Some({}.to_string())", quote!(#n)),
None => "None".to_owned(),
};
let target_state = match r.target_state {
Expand All @@ -450,15 +452,17 @@ pub fn lexerdef() -> {lexerdef_type} {{
r.name_span.start(),
r.name_span.end()
);
let regex = &r.re_str;
let start_states = r.start_states.as_slice();
write!(
outs,
"
Rule::new({}, {}, {}, \"{}\".to_string(), {:?}.to_vec(), {}).unwrap(),",
Rule::new({}, {}, {}, {}.to_string(), {}.to_vec(), {}).unwrap(),",
tok_id,
n,
n_span,
r.re_str.replace('\\', "\\\\").replace('"', "\\\""),
r.start_states,
quote!(#regex),
quote!([#(#start_states),*]),
target_state,
)
.ok();
Expand Down Expand Up @@ -652,10 +656,11 @@ pub fn ct_token_map<StorageT: Display>(
// forces a recompile, this will change this value, causing anything which depends on this
// build of lrlex to be recompiled too.
let mut outs = String::new();
let timestamp = env!("VERGEN_BUILD_TIMESTAMP");
write!(
outs,
"// lrlex build time: {:?}\n\nmod {} {{\n",
env!("VERGEN_BUILD_TIMESTAMP"),
"// lrlex build time: {}\n\nmod {} {{\n",
quote!(#timestamp),
mod_name
)
.ok();
Expand Down
15 changes: 15 additions & 0 deletions lrpar/cttests/src/quoting.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Test NoAction using the calculator grammar
yacckind: Original(YaccOriginalActionKind::NoAction)
grammar: |
%start S
%%
S: '\' | '"' | '<' | '+' '🦀' ;

lexer: |
%%
" '"'
\< '<'
\\ '\'
\+ '+'
🦀 '🦀'
[\t ]+ ;

0 comments on commit d005710

Please sign in to comment.