Skip to content

Commit

Permalink
added cst string support
Browse files Browse the repository at this point in the history
  • Loading branch information
lexa-diky committed May 8, 2024
1 parent 88b832f commit 37c28b3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/cst/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ fn parse_emit_statement(node: &AstNode) -> Result<CstEmitStatement, CstParserErr
for child in &node.children {
match child.node_type {
AstNodeType::AtomHex => parse_atom_hex_into(child, &mut atoms)?,
AstNodeType::AtomUtf8 => parse_atom_utf8_into(child, &mut atoms)?,
_ => panic!("Unexpected node type: {:?}", child.node_type)
}
}
Expand All @@ -91,6 +92,8 @@ fn parse_emit_statement(node: &AstNode) -> Result<CstEmitStatement, CstParserErr
}

fn parse_atom_hex_into(node: &AstNode, buf: &mut Vec<CstAtom>) -> Result<(), CstParserError> {
guard_node_type(node, AstNodeType::AtomHex)?;

let content = node.clone().content
.ok_or(CstParserError::MissingContent { node_type: AstNodeType::AtomHex })?;

Expand All @@ -106,6 +109,17 @@ fn parse_atom_hex_into(node: &AstNode, buf: &mut Vec<CstAtom>) -> Result<(), Cst
return Ok(());
}

fn parse_atom_utf8_into(node: &AstNode, buf: &mut Vec<CstAtom>) -> Result<(), CstParserError> {
guard_node_type(node, AstNodeType::AtomUtf8)?;

let content = node.clone().content
.ok_or(CstParserError::MissingContent { node_type: AstNodeType::AtomUtf8 })?;

buf.push(CstAtom::String(content));

return Ok(());
}

fn guard_node_type(node: &AstNode, expected_type: AstNodeType) -> Result<(), CstParserError> {
if node.node_type != expected_type {
return Err(
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn new_compiler() {

let source = StringCompilerSource::new(
PathBuf::from(""),
"> 01 02 03"
"> 01 02 03 'lol'"
);

let compilation_result = compiler.compile(source);
Expand Down

0 comments on commit 37c28b3

Please sign in to comment.