diff --git a/src/parser/mod.rs b/src/parser/mod.rs index ce51f9f..822f825 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -396,7 +396,7 @@ fn parse_ident<'input>(input: &mut Input<'input>) -> PResult<'input, TemplateTok } fn ident<'input>(input: &mut Input<'input>) -> PResult<'input, TempleInput> { - take_while(1.., char::is_alphanumeric).parse_next(input) + take_while(1.., |c: char| c.is_alphanumeric() || "_".contains(c)).parse_next(input) } fn bad_ident<'input>(input: &mut Input<'input>) -> PResult<'input, ()> { diff --git a/tests/cases/1-parsed@identifiers.snap b/tests/cases/1-parsed@identifiers.snap new file mode 100644 index 0000000..755d9ec --- /dev/null +++ b/tests/cases/1-parsed@identifiers.snap @@ -0,0 +1,178 @@ +--- +source: tests/file_tests.rs +expression: parsed +input_file: tests/cases/identifiers.temple +--- +ParsedTemplate { + tokens: [ + TemplateToken { + kind: LeftDelim, + source: "{{", + }, + TemplateToken { + kind: WantsOutput, + source: "=", + }, + TemplateToken { + kind: Whitespace, + source: " ", + }, + TemplateToken { + kind: Ident, + source: "_name", + }, + TemplateToken { + kind: Whitespace, + source: " ", + }, + TemplateToken { + kind: RightDelim, + source: "}}", + }, + TemplateToken { + kind: Whitespace, + source: " + ", + }, + TemplateToken { + kind: LeftDelim, + source: "{{", + }, + TemplateToken { + kind: WantsOutput, + source: "=", + }, + TemplateToken { + kind: Whitespace, + source: " ", + }, + TemplateToken { + kind: Ident, + source: "a_name", + }, + TemplateToken { + kind: Whitespace, + source: " ", + }, + TemplateToken { + kind: RightDelim, + source: "}}", + }, + TemplateToken { + kind: Whitespace, + source: " + ", + }, + TemplateToken { + kind: LeftDelim, + source: "{{", + }, + TemplateToken { + kind: WantsOutput, + source: "=", + }, + TemplateToken { + kind: Whitespace, + source: " ", + }, + TemplateToken { + kind: Ident, + source: "1name", + }, + TemplateToken { + kind: Whitespace, + source: " ", + }, + TemplateToken { + kind: RightDelim, + source: "}}", + }, + TemplateToken { + kind: Whitespace, + source: " + ", + }, + TemplateToken { + kind: LeftDelim, + source: "{{", + }, + TemplateToken { + kind: WantsOutput, + source: "=", + }, + TemplateToken { + kind: Whitespace, + source: " ", + }, + TemplateToken { + kind: Ident, + source: "_name1", + }, + TemplateToken { + kind: Whitespace, + source: " ", + }, + TemplateToken { + kind: RightDelim, + source: "}}", + }, + TemplateToken { + kind: Whitespace, + source: " + ", + }, + TemplateToken { + kind: LeftDelim, + source: "{{", + }, + TemplateToken { + kind: WantsOutput, + source: "=", + }, + TemplateToken { + kind: Whitespace, + source: " ", + }, + TemplateToken { + kind: Ident, + source: "_namE", + }, + TemplateToken { + kind: Whitespace, + source: " ", + }, + TemplateToken { + kind: RightDelim, + source: "}}", + }, + TemplateToken { + kind: Whitespace, + source: " + ", + }, + TemplateToken { + kind: LeftDelim, + source: "{{", + }, + TemplateToken { + kind: WantsOutput, + source: "=", + }, + TemplateToken { + kind: Whitespace, + source: " ", + }, + TemplateToken { + kind: Ident, + source: "name1", + }, + TemplateToken { + kind: Whitespace, + source: " ", + }, + TemplateToken { + kind: RightDelim, + source: "}}", + }, + ], +} diff --git a/tests/cases/2-ast@identifiers.snap b/tests/cases/2-ast@identifiers.snap new file mode 100644 index 0000000..abfc850 --- /dev/null +++ b/tests/cases/2-ast@identifiers.snap @@ -0,0 +1,135 @@ +--- +source: tests/file_tests.rs +expression: ast +input_file: tests/cases/identifiers.temple +--- +TemplateAst { + root: [ + Interpolation { + prev_whitespace: None, + wants_output: Some( + TemplateToken { + kind: WantsOutput, + source: "=", + }, + ), + expression: VariableAccess( + TemplateToken { + kind: Ident, + source: "_name", + }, + ), + post_whitespace: Some( + TemplateToken { + kind: Whitespace, + source: " + ", + }, + ), + }, + Interpolation { + prev_whitespace: None, + wants_output: Some( + TemplateToken { + kind: WantsOutput, + source: "=", + }, + ), + expression: VariableAccess( + TemplateToken { + kind: Ident, + source: "a_name", + }, + ), + post_whitespace: Some( + TemplateToken { + kind: Whitespace, + source: " + ", + }, + ), + }, + Interpolation { + prev_whitespace: None, + wants_output: Some( + TemplateToken { + kind: WantsOutput, + source: "=", + }, + ), + expression: VariableAccess( + TemplateToken { + kind: Ident, + source: "1name", + }, + ), + post_whitespace: Some( + TemplateToken { + kind: Whitespace, + source: " + ", + }, + ), + }, + Interpolation { + prev_whitespace: None, + wants_output: Some( + TemplateToken { + kind: WantsOutput, + source: "=", + }, + ), + expression: VariableAccess( + TemplateToken { + kind: Ident, + source: "_name1", + }, + ), + post_whitespace: Some( + TemplateToken { + kind: Whitespace, + source: " + ", + }, + ), + }, + Interpolation { + prev_whitespace: None, + wants_output: Some( + TemplateToken { + kind: WantsOutput, + source: "=", + }, + ), + expression: VariableAccess( + TemplateToken { + kind: Ident, + source: "_namE", + }, + ), + post_whitespace: Some( + TemplateToken { + kind: Whitespace, + source: " + ", + }, + ), + }, + Interpolation { + prev_whitespace: None, + wants_output: Some( + TemplateToken { + kind: WantsOutput, + source: "=", + }, + ), + expression: VariableAccess( + TemplateToken { + kind: Ident, + source: "name1", + }, + ), + post_whitespace: None, + }, + ], +} diff --git a/tests/cases/3-instructions@identifiers.snap b/tests/cases/3-instructions@identifiers.snap new file mode 100644 index 0000000..482d6a6 --- /dev/null +++ b/tests/cases/3-instructions@identifiers.snap @@ -0,0 +1,93 @@ +--- +source: tests/file_tests.rs +expression: emit +input_file: tests/cases/identifiers.temple +--- +[ + LoadFromContextToSlot { + name: "_name", + slot: VariableSlot { + index: 0, + }, + }, + EmitFromSlot { + slot: VariableSlot { + index: 0, + }, + }, + AppendContent { + content: " + ", + }, + LoadFromContextToSlot { + name: "a_name", + slot: VariableSlot { + index: 1, + }, + }, + EmitFromSlot { + slot: VariableSlot { + index: 1, + }, + }, + AppendContent { + content: " + ", + }, + LoadFromContextToSlot { + name: "1name", + slot: VariableSlot { + index: 2, + }, + }, + EmitFromSlot { + slot: VariableSlot { + index: 2, + }, + }, + AppendContent { + content: " + ", + }, + LoadFromContextToSlot { + name: "_name1", + slot: VariableSlot { + index: 3, + }, + }, + EmitFromSlot { + slot: VariableSlot { + index: 3, + }, + }, + AppendContent { + content: " + ", + }, + LoadFromContextToSlot { + name: "_namE", + slot: VariableSlot { + index: 4, + }, + }, + EmitFromSlot { + slot: VariableSlot { + index: 4, + }, + }, + AppendContent { + content: " + ", + }, + LoadFromContextToSlot { + name: "name1", + slot: VariableSlot { + index: 5, + }, + }, + EmitFromSlot { + slot: VariableSlot { + index: 5, + }, + }, +] diff --git a/tests/cases/4-output@identifiers.snap b/tests/cases/4-output@identifiers.snap new file mode 100644 index 0000000..67a3f6e --- /dev/null +++ b/tests/cases/4-output@identifiers.snap @@ -0,0 +1,6 @@ +--- +source: tests/file_tests.rs +expression: output +input_file: tests/cases/identifiers.temple +--- +"Foo\nFoo\nFoo\nFoo\nFoo\nFoo" diff --git a/tests/cases/identifiers.temple b/tests/cases/identifiers.temple new file mode 100644 index 0000000..570a7b2 --- /dev/null +++ b/tests/cases/identifiers.temple @@ -0,0 +1,15 @@ +{ + "_name": "Foo", + "a_name": "Foo", + "1name": "Foo", + "_name1": "Foo", + "_namE": "Foo", + "name1": "Foo" +} +--- +{{= _name }} +{{= a_name }} +{{= 1name }} +{{= _name1 }} +{{= _namE }} +{{= name1 }} \ No newline at end of file