Skip to content

Commit

Permalink
shin-asm: fix parsing of instruction block and instruction args
Browse files Browse the repository at this point in the history
- stop parsing instruction list when expr can no longer be parsed
- allow labels only at the start of the instruction block
  • Loading branch information
DCNick3 committed Sep 1, 2023
1 parent 347c797 commit dfb5fb7
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 21 deletions.
22 changes: 12 additions & 10 deletions shin-asm/src/parser/grammar/items/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ use super::*;
pub(super) fn instructions_block(p: &mut Parser<'_>) {
let m = p.start();

while p.at(IDENT) {
if p.at(IDENT) {
instruction_or_label(p);
}

while p.at(IDENT) && !p.nth_at(1, T![:]) {
instruction_or_label(p);
}

Expand Down Expand Up @@ -51,15 +55,13 @@ fn instruction(p: &mut Parser<'_>) {
fn instr_arg_list(p: &mut Parser<'_>) {
let m = p.start();

while !p.at_ts(EOL_SET) {
delimited(
p,
EOL_SET,
T![,],
expressions::EXPR_FIRST,
|p: &mut Parser<'_>| expressions::expr(p).is_some(),
);
}
delimited(
p,
EOL_SET,
T![,],
expressions::EXPR_FIRST,
|p: &mut Parser<'_>| expressions::expr(p).is_some(),
);

m.complete(p, INSTR_ARG_LIST);
}
94 changes: 94 additions & 0 deletions shin-asm/test_data/parser/err/0001_junk.sast
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
SOURCE_FILE
WHITESPACE " "
INSTRUCTIONS_BLOCK
LABEL
IDENT "choice_set_base"
COLON ":"
WHITESPACE " "
INSTRUCTION
INSTRUCTION_NAME
IDENT "u16"
ERROR
COMMA ","
NEWLINE "\n"
WHITESPACE " "
LABEL
IDENT "choice_index"
COLON ":"
WHITESPACE " "
INSTRUCTION
INSTRUCTION_NAME
IDENT "u16"
ERROR
COMMA ","
NEWLINE "\n"
WHITESPACE " "
ERROR
ERROR "#"
ERROR
L_BRACK "["
INSTRUCTIONS_BLOCK
INSTRUCTION
INSTRUCTION_NAME
IDENT "cmd"
INSTR_ARG_LIST
PAREN_EXPR
L_PAREN "("
NAME_REF_EXPR
IDENT "dest"
R_PAREN ")"
ERROR
R_BRACK "]"
NEWLINE "\n"
WHITESPACE " "
LABEL
IDENT "dest"
COLON ":"
WHITESPACE " "
INSTRUCTION
INSTRUCTION_NAME
IDENT "MemoryAddress"
ERROR
COMMA ","
NEWLINE "\n"
WHITESPACE " "
LABEL
IDENT "choice_visibility_mask"
COLON ":"
WHITESPACE " "
INSTRUCTION
INSTRUCTION_NAME
IDENT "NumberSpec"
ERROR
COMMA ","
NEWLINE "\n"
WHITESPACE " "
LABEL
IDENT "choice_title"
COLON ":"
WHITESPACE " "
INSTRUCTION
INSTRUCTION_NAME
IDENT "U16String"
ERROR
COMMA ","
NEWLINE "\n"
WHITESPACE " "
LABEL
IDENT "variants"
COLON ":"
WHITESPACE " "
INSTRUCTION
INSTRUCTION_NAME
IDENT "StringArray"
ERROR
COMMA ","
error 28: expected a newline
error 55: expected a newline
error 65: expected an instruction or label
error 66: expected an instruction or label
error 76: expected a newline
error 105: expected a newline
error 149: expected a newline
error 182: expected a newline
error 213: expected a newline
15 changes: 4 additions & 11 deletions shin-asm/test_data/parser/ok/0016_label_split.sal
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
LABEL_2:
add $1, -1, 14
jt $v0, {
0 => SNR_0,
1 => SNR_1,
}
LABEL_3:
SELECT 1, 2, $choice, 14, "NCSELECT", [
"To Be",
"Not to Be",
]
BLOCK_1:
hello
BLOCK_2:
world
20 changes: 20 additions & 0 deletions shin-asm/test_data/parser/ok/0016_label_split.sast
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
SOURCE_FILE
INSTRUCTIONS_BLOCK
LABEL
IDENT "BLOCK_1"
COLON ":"
NEWLINE "\n"
WHITESPACE " "
INSTRUCTION
INSTRUCTION_NAME
IDENT "hello"
NEWLINE "\n"
INSTRUCTIONS_BLOCK
LABEL
IDENT "BLOCK_2"
COLON ":"
NEWLINE "\n"
WHITESPACE " "
INSTRUCTION
INSTRUCTION_NAME
IDENT "world"

0 comments on commit dfb5fb7

Please sign in to comment.