-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Parser should be Lexer, and Compiler should be Parser... -_-
- Loading branch information
Showing
14 changed files
with
427 additions
and
399 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import gleam/result | ||
import gleam/string | ||
|
||
pub type Token { | ||
Constant(start: Int, end: Int, value: String) | ||
Property(start: Int, end: Int, path: List(String)) | ||
BlockStart(start: Int, end: Int, kind: String, path: List(String)) | ||
BlockEnd(start: Int, end: Int, kind: String) | ||
} | ||
|
||
pub type LexError { | ||
UnexpectedToken(index: Int, str: String) | ||
UnexpectedEof(index: Int) | ||
SyntaxError(errors: List(SyntaxError)) | ||
} | ||
|
||
pub type SyntaxError { | ||
EmptyExpression(start: Int, end: Int) | ||
MissingBlockKind(start: Int, end: Int) | ||
UnexpectedBlockArgument(start: Int, end: Int) | ||
} | ||
|
||
type ParserState { | ||
Static(start: Int, end: Int, str: String) | ||
Tag(start: Int, end: Int, str: String) | ||
TagStart(start: Int) | ||
TagEnd(start: Int) | ||
} | ||
|
||
fn step( | ||
state: ParserState, | ||
acc: List(Result(Token, SyntaxError)), | ||
input: String, | ||
) -> Result(List(Result(Token, SyntaxError)), LexError) { | ||
case state { | ||
Static(start, end, str) -> | ||
case string.first(input) { | ||
Ok("{") -> | ||
step( | ||
TagStart(end + 1), | ||
[Ok(Constant(start, end, str)), ..acc], | ||
string.drop_left(input, 1), | ||
) | ||
Ok(char) -> | ||
step( | ||
Static(start, end + 1, string.append(str, char)), | ||
acc, | ||
string.drop_left(input, 1), | ||
) | ||
Error(_) -> Ok([Ok(Constant(start, end, str)), ..acc]) | ||
} | ||
Tag(start, end, value) -> | ||
case string.first(input) { | ||
Ok("}") -> | ||
step( | ||
TagEnd(end + 1), | ||
[ | ||
{ | ||
let val = string.trim(value) | ||
case string.first(val) { | ||
Ok("#") -> | ||
case string.split_once(string.drop_left(val, 1), " ") { | ||
Ok(#(kind, body)) -> | ||
Ok(BlockStart(start, end, kind, string.split(body, "."))) | ||
Error(_) -> Error(MissingBlockKind(start, end)) | ||
} | ||
Ok("/") -> | ||
case string.split_once(string.drop_left(val, 1), " ") { | ||
Ok(#(_, _)) -> Error(UnexpectedBlockArgument(start, end)) | ||
Error(_) -> | ||
Ok(BlockEnd(start, end, string.drop_left(val, 1))) | ||
} | ||
Ok(_) -> Ok(Property(start, end, string.split(value, "."))) | ||
Error(_) -> Error(EmptyExpression(start, end)) | ||
} | ||
}, | ||
..acc | ||
], | ||
string.drop_left(input, 1), | ||
) | ||
Ok(char) -> | ||
step( | ||
Tag(start, end + 1, string.append(value, char)), | ||
acc, | ||
string.drop_left(input, 1), | ||
) | ||
Error(_) -> Error(UnexpectedEof(end)) | ||
} | ||
TagStart(start) -> | ||
case string.first(input) { | ||
Ok("{") -> | ||
step(Tag(start + 1, start + 1, ""), acc, string.drop_left(input, 1)) | ||
Ok(char) -> Error(UnexpectedToken(start, char)) | ||
Error(_) -> Error(UnexpectedEof(start)) | ||
} | ||
TagEnd(start) -> | ||
case string.first(input) { | ||
Ok("}") -> | ||
step( | ||
Static(start + 1, start + 1, ""), | ||
acc, | ||
string.drop_left(input, 1), | ||
) | ||
Ok(char) -> Error(UnexpectedToken(start, char)) | ||
Error(_) -> Error(UnexpectedEof(start)) | ||
} | ||
} | ||
} | ||
|
||
pub fn run(template: String) -> Result(List(Token), LexError) { | ||
case step(Static(0, 0, ""), [], template) { | ||
Ok(tokens) -> | ||
case | ||
tokens | ||
|> result.partition | ||
{ | ||
#(ok, []) -> Ok(ok) | ||
#(_, err) -> Error(SyntaxError(err)) | ||
} | ||
Error(err) -> Error(err) | ||
} | ||
} |
Oops, something went wrong.