Skip to content

Commit

Permalink
temporary error catching for escaping single quote and unicode; tag 0…
Browse files Browse the repository at this point in the history
….1.14
  • Loading branch information
tiye committed Nov 6, 2021
1 parent 39a43b4 commit 1039d45
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 8 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cirru_parser"
version = "0.1.13"
version = "0.1.14"
authors = ["jiyinyiyong <[email protected]>"]
edition = "2018"
license = "MIT"
Expand All @@ -21,7 +21,7 @@ exclude = [
[dependencies]

[dev-dependencies]
serde_json = "1.0.68"
serde_json = "1.0.69"
criterion = "0.3"


Expand Down
2 changes: 1 addition & 1 deletion benches/parsing.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use criterion::{criterion_group, criterion_main, Criterion};
use std::fs;

use cirru_parser::parse;
Expand Down
2 changes: 2 additions & 0 deletions examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ defn fib (n)
);

let large_demo = "/Users/chen/repo/calcit-lang/editor/compact.cirru";
// let large_demo = "/Users/chen/repo/calcit-lang/respo-calcit-workflow/js-out/program-ir.cirru";
// let large_demo = "/Users/chen/repo/calcit-lang/calcit_runner.rs/js-out/program-ir.cirru";
let content = fs::read_to_string(large_demo).unwrap();

match parse(&content) {
Expand Down
6 changes: 6 additions & 0 deletions examples/escape.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use cirru_parser::{lex, parse, Cirru};

pub fn main() {
println!("{:?}", parse("a \"b\\u{87DF}\""));
println!("{:?}", parse("a \"b\\u{87DF}\" c d e f g f"));
}
26 changes: 24 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub fn lex(initial_code: &str) -> Result<CirruLexItemList, String> {
let mut buffer = String::from("");
let code = initial_code;

for c in code.chars() {
for (idx, c) in code.chars().enumerate() {
match state {
CirruLexState::Space => match c {
' ' => {
Expand Down Expand Up @@ -196,6 +196,10 @@ pub fn lex(initial_code: &str) -> Result<CirruLexItemList, String> {
state = CirruLexState::Str;
buffer.push('"');
}
'\'' => {
state = CirruLexState::Str;
buffer.push('\'');
}
't' => {
state = CirruLexState::Str;
buffer.push('\t');
Expand All @@ -204,11 +208,29 @@ pub fn lex(initial_code: &str) -> Result<CirruLexItemList, String> {
state = CirruLexState::Str;
buffer.push('\n');
}
'u' => {
// not supporting, but don't panic
let end = idx + 10;
let peek = if end >= code.len() {
&code[idx..]
} else {
&code[idx..end]
};
println!("Unicode escaping is not supported yet: {:?} ...", peek);
buffer.push('\\');
buffer.push('u');
state = CirruLexState::Str;
}
'\\' => {
state = CirruLexState::Str;
buffer.push('\\');
}
_ => return Err(String::from("unexpected character during string escaping")),
_ => {
return Err(format!(
"unexpected character during string escaping: {:?}",
c
))
}
},
CirruLexState::Indent => match c {
' ' => {
Expand Down
20 changes: 20 additions & 0 deletions tests/lexer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,23 @@ fn lex_strings() -> Result<(), String> {

Ok(())
}

#[test]
fn escape_chars() -> Result<(), String> {
assert_eq!(
lex(r#""\u{6c49}""#)?,
vec![
CirruLexItem::Indent(0),
CirruLexItem::Str(String::from(r#"\u{6c49}"#))
]
);

assert_eq!(
lex(r#""\'""#)?,
vec![
CirruLexItem::Indent(0),
CirruLexItem::Str(String::from(r#"'"#))
]
);
Ok(())
}

0 comments on commit 1039d45

Please sign in to comment.