pawarser, json-pawarser: get first debug print working!
This commit is contained in:
parent
af6886214b
commit
b8720b2df9
7 changed files with 140 additions and 23 deletions
|
@ -4,8 +4,8 @@ use crate::{syntax_error::SyntaxError, syntax_kind::SyntaxKind};
|
|||
|
||||
use self::object::object;
|
||||
|
||||
type Parser<'src> = pawarser::Parser<'src, SyntaxKind, SyntaxError>;
|
||||
type CompletedMarker = pawarser::CompletedMarker<SyntaxKind, SyntaxError>;
|
||||
pub(crate) type Parser<'src> = pawarser::Parser<'src, SyntaxKind, SyntaxError>;
|
||||
pub(crate) type CompletedMarker = pawarser::CompletedMarker<SyntaxKind, SyntaxError>;
|
||||
|
||||
const BASIC_VALUE_TOKENS: EnumSet<SyntaxKind> =
|
||||
enum_set!(SyntaxKind::BOOL | SyntaxKind::NULL | SyntaxKind::NUMBER | SyntaxKind::STRING);
|
||||
|
@ -27,12 +27,15 @@ mod object {
|
|||
pub(super) fn object(p: &mut Parser) -> Option<CompletedMarker> {
|
||||
let obj_start = p.start("object");
|
||||
|
||||
if !p.at(SyntaxKind::BRACE_OPEN) {
|
||||
if !p.eat(SyntaxKind::BRACE_OPEN) {
|
||||
obj_start.abandon(p);
|
||||
return None;
|
||||
}
|
||||
|
||||
todo!()
|
||||
member(p);
|
||||
|
||||
p.eat(SyntaxKind::BRACE_CLOSE);
|
||||
Some(obj_start.complete(p, SyntaxKind::OBJECT))
|
||||
}
|
||||
|
||||
fn member(p: &mut Parser) -> Option<CompletedMarker> {
|
||||
|
@ -46,7 +49,7 @@ mod object {
|
|||
p.eat(SyntaxKind::STRING);
|
||||
member_name_start.complete(p, SyntaxKind::MEMBER_NAME);
|
||||
} else {
|
||||
return todo!("handle other tokens");
|
||||
return todo!("handle other tokens: {:?}", p.current());
|
||||
}
|
||||
|
||||
if !p.eat(SyntaxKind::COLON) {
|
||||
|
|
|
@ -1,3 +1,29 @@
|
|||
mod grammar;
|
||||
mod syntax_error;
|
||||
mod syntax_kind;
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use pawarser::parser::ParserBuilder;
|
||||
|
||||
use crate::{
|
||||
grammar::{value, Parser},
|
||||
syntax_kind::{lex, SyntaxKind},
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
const TEST_DATA: &str = r#"{"hello_world": "meow"}"#;
|
||||
let toks = lex(TEST_DATA);
|
||||
|
||||
let mut p: Parser = ParserBuilder::new(toks)
|
||||
.add_meaningless(SyntaxKind::WHITESPACE)
|
||||
.add_meaningless(SyntaxKind::NEWLINE)
|
||||
.build();
|
||||
|
||||
value(&mut p);
|
||||
|
||||
let out = p.finish();
|
||||
assert_eq!("", format!("{:#?}", out))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::syntax_kind::SyntaxKind;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum SyntaxError {
|
||||
DisallowedKeyType(SyntaxKind),
|
||||
MemberMissingValue,
|
||||
|
|
|
@ -16,14 +16,6 @@ pub fn lex(src: &str) -> Vec<(SyntaxKind, &str)> {
|
|||
#[enumset(no_super_impls)]
|
||||
#[allow(non_camel_case_types)]
|
||||
pub enum SyntaxKind {
|
||||
// Error SyntaxKinds
|
||||
LEX_ERR,
|
||||
PARSE_ERR,
|
||||
|
||||
// Meta SyntaxKinds
|
||||
TOMBSTONE,
|
||||
EOF,
|
||||
|
||||
OBJECT,
|
||||
MEMBER,
|
||||
MEMBER_NAME,
|
||||
|
@ -61,6 +53,13 @@ pub enum SyntaxKind {
|
|||
WHITESPACE,
|
||||
#[token("\n")]
|
||||
NEWLINE,
|
||||
|
||||
// Error SyntaxKinds
|
||||
LEX_ERR,
|
||||
PARSE_ERR,
|
||||
|
||||
// Meta SyntaxKinds
|
||||
EOF,
|
||||
}
|
||||
|
||||
impl pawarser::parser::SyntaxElement for SyntaxKind {
|
||||
|
@ -75,6 +74,16 @@ impl From<SyntaxKind> for rowan::SyntaxKind {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<rowan::SyntaxKind> for SyntaxKind {
|
||||
fn from(raw: rowan::SyntaxKind) -> Self {
|
||||
assert!(raw.0 <= SyntaxKind::EOF as u16);
|
||||
#[allow(unsafe_code, reason = "The transmute is necessary here")]
|
||||
unsafe {
|
||||
std::mem::transmute::<u16, SyntaxKind>(raw.0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::syntax_kind::{lex, SyntaxKind};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue