Skip to content

Commit 1c9866c

Browse files
committed
Support comment, empty/blank line, and whitespace
1 parent 09ef25b commit 1c9866c

File tree

6 files changed

+47
-9
lines changed

6 files changed

+47
-9
lines changed

.github/main.workflow

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ action "Perf cargo" {
4848
"build --all",
4949
"build -p scdlang-core",
5050
"build -p scrap",
51+
"build -p scdlang_xstate",
5152
]
5253
}
5354

examples/simple.scl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/// simple exmaple to test if it work
2+
/// this example contain empty & blank line, whitespace, tab, oneline & block comment
3+
4+
// without whitespace
15
A->B
2-
B->C
3-
C->D
6+
7+
// with whitespace
8+
B -> C
9+
10+
C/*for cheetah*/ -> D//for dimple
11+

packages/cli/src/commands/eval/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ impl<'c> CLI<'c> for Eval {
3737
println!("Press Ctrl-D to exit and print the final results");
3838
}
3939

40+
// TODO: change to https://docs.rs/linefeed
4041
prompting();
4142
for line in stdin.lock().lines() {
4243
let expression = line.expect(Self::NAME);

packages/core/src/grammar.pest

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@ DescriptionFile = { SOI ~ (
22
NEWLINE? ~ (
33
expression
44
) ~ NEWLINE?
5-
)+ ~ EOI }
5+
)* ~ EOI }
66

77
expression = { StateName ~ transition }
88
transition = { TransitionTo ~ StateName }
99

1010
// #region symbol
11-
TransitionTo = { "-"+ ~ ">" }
11+
TransitionTo = @{ "-"+ ~ ">" }
1212
// #endregion
1313

1414
// #region name
15-
StateName = { ASCII_ALPHANUMERIC+ }
16-
// #endregion
15+
StateName = @{ ASCII_ALPHANUMERIC+ }
16+
// #endregion
17+
18+
WHITESPACE = _{ " " | "\t" }
19+
COMMENT = _{ line_comment | block_comment }
20+
line_comment = _{ "//" ~ (!NEWLINE ~ ANY)* }
21+
block_comment = _{ "/*" ~ (block_comment|(!"*/" ~ ANY))* ~ "*/" }

packages/transpiler/xstate/src/ast/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,12 @@ impl Machine {
4949

5050
pub fn from(source: &str) -> Result<Machine, MachineError> {
5151
let mut parse_tree = scdlang::parse(&source)?;
52-
let ast = Machine::from_pest(&mut parse_tree).expect("infallible");
53-
Ok(ast)
52+
if pairs::is_expression(&parse_tree) {
53+
let line = &format!(r#"expression("{line}")"#, line = parse_tree.as_str());
54+
Ok(Machine::from_pest(&mut parse_tree).expect(line))
55+
} else {
56+
Ok(Machine::default())
57+
}
5458
}
5559

5660
pub fn parse(&mut self, source: &str) -> Result<(), MachineError> {

packages/transpiler/xstate/src/ast/utils.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
1+
use scdlang_core::Rule;
2+
3+
pub mod pairs {
4+
use super::*;
5+
use pest::iterators::Pairs;
6+
7+
pub fn is_expression(pairs: &Pairs<Rule>) -> bool {
8+
pairs
9+
.peek()
10+
.unwrap()
11+
.into_inner()
12+
.any(|pair| match pair.as_rule() {
13+
Rule::expression => true,
14+
_ => false,
15+
})
16+
}
17+
}
18+
119
pub mod span {
20+
use super::*;
221
use crate::ast::Transition;
322
use from_pest::FromPest;
423
use pest::{Parser, Span};
5-
use scdlang_core::{Rule, Scdlang};
24+
use scdlang_core::Scdlang;
625
use serde_json::Value;
726
use std::collections::HashMap;
827

0 commit comments

Comments
 (0)