Skip to content

Commit

Permalink
fix: fixed regex for string value in json examples
Browse files Browse the repository at this point in the history
  • Loading branch information
pamburus committed Jan 20, 2025
1 parent 5e5046c commit ddc6ba0
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 5 deletions.
3 changes: 2 additions & 1 deletion book/src/examples/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Knowing that, we can construct a lexer with `Logos` that will identify all those
```admonish note
The hardest part is to define valid regexes for `Number` and `String` variants.
The present solution was inspired by
[this stackoverflow thread](https://stackoverflow.com/questions/32155133/regex-to-match-a-json-string).
[this stackoverflow thread](https://stackoverflow.com/questions/32155133/regex-to-match-a-json-string)
and checked against [the JSON specification](https://www.json.org/json-en.html).
```

Once we have our tokens, we must parse them into actual JSON values. We will proceed be creating 3 functions:
Expand Down
4 changes: 2 additions & 2 deletions book/src/examples/json_borrowed.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ is straightforward:
- enum Token {
+ enum Token<'source> {
@ 62,63c58,59
- #[regex(r#""([^"\\]|\\["\\bnfrt]|u[a-fA-F0-9]{4})*""#, |lex| lex.slice().to_owned())]
- #[regex(r#""([^"\\\x00-\x1F]|\\(["\\bnfrt/]|u[a-fA-F0-9]{4}))*""#, |lex| lex.slice().to_owned())]
- String(String),
+ #[regex(r#""([^"\\]|\\["\\bnfrt]|u[a-fA-F0-9]{4})*""#, |lex| lex.slice())]
+ #[regex(r#""([^"\\\x00-\x1F]|\\(["\\bnfrt/]|u[a-fA-F0-9]{4}))*""#, |lex| lex.slice())]
+ String(&'source str),
@ 70c66
- enum Value {
Expand Down
2 changes: 1 addition & 1 deletion examples/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ enum Token {
#[regex(r"-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?", |lex| lex.slice().parse::<f64>().unwrap())]
Number(f64),

#[regex(r#""([^"\\]|\\["\\bnfrt]|u[a-fA-F0-9]{4})*""#, |lex| lex.slice().to_owned())]
#[regex(r#""([^"\\\x00-\x1F]|\\(["\\bnfrt/]|u[a-fA-F0-9]{4}))*""#, |lex| lex.slice().to_owned())]
String(String),
}
/* ANCHOR_END: tokens */
Expand Down
2 changes: 1 addition & 1 deletion examples/json_borrowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ enum Token<'source> {
#[regex(r"-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?", |lex| lex.slice().parse::<f64>().unwrap())]
Number(f64),

#[regex(r#""([^"\\]|\\["\\bnfrt]|u[a-fA-F0-9]{4})*""#, |lex| lex.slice())]
#[regex(r#""([^"\\\x00-\x1F]|\\(["\\bnfrt/]|u[a-fA-F0-9]{4}))*""#, |lex| lex.slice())]
String(&'source str),
}
/* ANCHOR_END: tokens */
Expand Down

0 comments on commit ddc6ba0

Please sign in to comment.