Added Lua parser implementation and Lua BNF through macro_rules!;

Added a windowed application shell from a tutorial using wgpu;
This commit is contained in:
2026-06-10 02:27:51 +01:00
parent 6e7201bd4e
commit ce2bd0b360
4 changed files with 360 additions and 41 deletions
+299
View File
@@ -0,0 +1,299 @@
macro_rules! definition {
{@build ($n:ident structure) ($($t:tt)*) null () } => {
struct $n ($($t)*);
};
{@build ($n:ident enumeration new ($($f:tt)*) ($m:ident($($mt:tt)*)$($r:tt)*)) () null ()} => {
definition!{@build ($n enumeration ($($f)*) ($m($($mt)*)$($r)*)) () null () $($mt)*}
};
{@build ($n:ident enumeration ($($f:tt)*) ($m:ident($($mt:tt)*)$($r:tt)*)) ($($t:tt)*) null ()} => {
definition!{@build ($n enumeration new ($($f)*$m($($t)*)) ($($r)*)) () null () }
};
{@build ($n:ident enumeration new ($($m:ident($($mt:tt)*))*) ()) () null () } => {
definition!{@build ($n enumeration filter () ($($m($($mt)*))*))}
};
{@build ($n:ident enumeration filter ($($e:tt)*) ($m:ident($($mt:tt)+)$($t:tt)*))} => {
definition!{@build ($n enumeration filter ($($e)*$m($($mt)*),) ($($t)*))}
};
{@build ($n:ident enumeration filter ($($e:tt)*) ($m:ident()$($t:tt)*))} => {
definition!{@build ($n enumeration filter ($($e)*$m,) ($($t)*))}
};
{@build ($n:ident enumeration filter ($($e:tt)*) ())} => {
enum $n {
$($e)*
}
};
{@build $m:tt ($($t:tt)*) null () $l:literal $($r:tt)*} => {
definition!{@build $m ($($t)*) null () $($r)*}
};
{@build $m:tt ($($t:tt)*) null () $i:ident $($r:tt)*} => {
definition!{@build $m ($($t)*Box<$i>,) null () $($r)*}
};
{@build $m:tt ($($t:tt)*) option ($a:tt) () $($r:tt)*} => {
definition!{@build $m ($($t)*Option<$a>,) null () $($r)*}
};
{@build $m:tt ($($t:tt)*) vector ($a:tt) () $($r:tt)*} => {
definition!{@build $m ($($t)*Vec<$a>,) null () $($r)*}
};
{@build $m:tt ($($t:tt)*) option ($($a:tt)*) () $($r:tt)*} => {
definition!{@build $m ($($t)*Option<($($a),*)>,) null () $($r)*}
};
{@build $m:tt ($($t:tt)*) vector ($($a:tt)*) () $($r:tt)*} => {
definition!{@build $m ($($t)*Vec<($($a),*)>,) null () $($r)*}
};
{@build $m:tt ($($t:tt)*) null () [$($b:tt)*] $($r:tt)*} => {
definition!{@build $m ($($t)*) option () ($($b)*) $($r)*}
};
{@build $m:tt ($($t:tt)*) null () {$($b:tt)*} $($r:tt)*} => {
definition!{@build $m ($($t)*) vector () ($($b)*) $($r)*}
};
{@build $m:tt ($($t:tt)*) $i:ident ($($a:tt)*) ($l:literal $($b:tt)*) $($r:tt)*} => {
definition!{@build $m ($($t)*) $i ($($a)*) ($($b)*) $($r)*}
};
{@build $m:tt ($($t:tt)*) $i:ident ($($a:tt)*) ($n:ident $($b:tt)*) $($r:tt)*} => {
definition!{@build $m ($($t)*) $i ($($a)*$n) ($($b)*) $($r)*}
};
}
macro_rules! implement {
{@begin $n:ident $i:ident $($t:tt)*} => {
implement!{@build ($i $n) {} {} $($t)*}
};
{@build $m:tt $f:tt {$($f2:tt)*} $s:literal $($t:tt)*} => {
implement!{@build $m $f {
$($f2)*
state = state.trim_start();
if state.starts_with($s) {
state = &state[$s.len()..]
} else {
return Err("error".into())
}
} $($t)*}
};
{@build $m:tt {$($f:tt)*} {$($f2:tt)*} $i:ident $($t:tt)*} => {
implement!{@build $m {$($f)*{$($f2)*}} {
let mut result;
match $i::take(state) {
Ok((r,s)) => {
state = s;
result = r;
}
Err(error) => {
return Err(error);
}
}
} $($t)*}
};
{@build ($($m:tt)*) {$($f:tt)*} {$($f2:tt)*} {$($r:tt)*} $($t:tt)*} => {
implement!{@build (vector {$($f)*{$($f2)*}} ($($t)*) $($m)*) {} {} $($r)*}
};
{@build ($($m:tt)*) {$($f:tt)*} {$($f2:tt)*} [$($r:tt)*] $($t:tt)*} => {
implement!{@build (option {$($f)*{$($f2)*}} ($($t)*) $($m)*) {} {} $($r)*}
};
{@finish (option {$($l:tt)*} ($($t:tt)*) $($m:tt)*) {{$($f:tt)*}$({$($r:tt)*})*} } => {
implement!{@build ($($m)*) {$($l)*} {
let mut result;
let taker = |_state: &str| {
let mut state: &str = _state;
$($f)*
Ok(($({$($r)*result}),*))
};
result = taker(state).ok();
} $($t)*}
};
{@finish (vector {$($l:tt)*} ($($t:tt)*) $($m:tt)*) {{$($f:tt)*}$({$($r:tt)*})*} } => {
implement!{@build ($($m)*) {$($l)*} {
let mut result = Vec::new();
let taker = |_state: &str| {
let mut state: &str = _state;
$($f)*
Ok(($({$($r)*result}),*))
};
loop {
match taker(state) {
Ok((r,s)) => {
state = s;
result.push(r);
}
Err(error) => {
break;
}
};
}
} $($t)*}
};
{@build $m:tt {$($r:tt)*} $r2:tt} => {
implement!{@finish $m {$($r)*$r2}}
};
{@finish (structure $n:ident) {{$($f:tt)*}$({$($r:tt)*})*} } => {
impl Parse for $n {
fn take(_state: &str) -> Result<(Self, &str), Error> where Self: Sized {
let mut state: &str = _state;
$($f)*
Ok(($n($({$($r)*result.into()}),*),state))
}
}
};
}
macro_rules! class {
($i:ident($($t:tt)*)) => {
definition!{@build ($i structure) () null () $($t)*}
implement!{@begin $i structure $($t)*}
};
($i:ident{$($n:ident($($t:tt)*)),*}) => {
definition!{@build ($i enumeration new () ($($n($($t)*))*)) () null ()}
}
}
macro_rules! parser {
() => {};
($i:ident$t:tt;$($tail:tt)*) => {
class!($i$t);
parser!($($tail)*);
};
}
struct Number(String);
struct Name(String);
type Error = String;
trait Parse {
fn take(state: &str) -> Result<(Self, &str), Error>
where Self: Sized;
}
impl Parse for String {
fn take(state: &str) -> Result<(Self, &str), Error>
where
Self: Sized
{
todo!()
}
}
fn g() -> Result<Chunk,()> {
Ok(Chunk(
{
if 1 == 2 {
Box::new(Block(Vec::new(),None))
} else {
return Err(())
}
}
))
}
parser! {
Chunk3 ( "Pookie" Block "Dookie" );
}
parser! {
Block2 ( {Statement} [Return] );
}
parser! {
// LiteralString -> "..."
// LiteralNumber -> 0x... 123.456
Chunk ( Block );
Block ( {Statement} [Return] );
Statement {
Semicolon (";"),
Assignment (Variables "=" Expressions),
Call (FunctionCall),
Label (Label),
Break ("break"),
Goto ("goto" Name),
Do ("do" Block "end"),
While ("while" Expression "do" Block "end"),
Repeat ("repeat" Block "until" Expression),
If ("if" Expression "then" Block {"elseif" Expression "then" Block} ["else" Block] "end"),
ForRange ("for" Name "=" Expression "," Expression ["," Expression] "do" Block "end"),
ForIn ("for" Names "in" Expressions "do" Block "end"),
Function ("function" FunctionName FunctionBody),
LocalFunction ("local" "function" Name FunctionBody),
GlobalFunction ("global" "function" Name FunctionBody),
Declaration ("local" Names ["=" Expressions]),
Global ("global" Names)
};
Return( "return" [Expressions] [";"] );
Label( "::" Name "::" );
FunctionName( Name {"." Name} [":" Name] );
Variables( Variable {"," Variable} );
Variable {
Name (Name),
Index (PrefixExpression "[" Expression "]"),
DotIndex (PrefixExpression "." Name)
};
Names( Name {"," Name} );
Expressions( Expression {"," Expression} );
Expression {
Nil ("nil"),
False ("false"),
True ("true"),
Number (Number),
String (String),
VarArg ("..."),
FunctionDef (FunctionDef),
PrefixExpression (PrefixExpression),
Table (Table),
Binary (Expression BinaryOp Expression),
Unary (UnaryOp Expression)
};
PrefixExpression {
Variable (Variable),
Call (FunctionCall),
Expression ("(" Expression ")")
};
FunctionCall {
Normal (PrefixExpression Arguments),
Instanced (PrefixExpression ":" Name Arguments)
};
Arguments {
Expressions ("(" [Expressions] ")"),
Table (Table),
String (String)
};
FunctionDef( "function" FunctionBody );
FunctionBody( "(" [Parameters] ")" Block "end" );
Parameters {
Names (Names ["," VarArg]),
VarArg (VarArg)
};
VarArg( "..." [Name] );
Table( "{" [Fields] "}" );
Fields( Field {FieldSep Field} [FieldSep] );
Field {
ExpressionIndex ("[" Expression "]" "=" Expression),
Name (Name "=" Expression),
Expression (Expression)
};
FieldSep {
Comma (","),
Semicolon (";")
};
BinaryOp {
Plus ("+"),
Minus ("-"),
Mul ("*"),
Divide ("/"),
DivFloor ("//"),
Caret ("^"),
Modulo ("%"),
Ampersand ("&"),
Tilde ("~"),
Pipe ("|"),
ShiftRight (">>"),
ShiftLeft ("<<"),
Concat (".."),
Less ("<"),
LessEqual ("<="),
Greater (">"),
GreatEqual (">="),
Equivalent ("=="),
NotEqual ("~="),
And ("and"),
Or ("or")
};
UnaryOp {
Negate ("-"),
Not ("!"),
Ampersand ("#"),
Tilde ("~")
};
}