Skip to content

Commit

Permalink
Support comment line
Browse files Browse the repository at this point in the history
  • Loading branch information
oovm committed Nov 28, 2023
1 parent e23addc commit 6443781
Show file tree
Hide file tree
Showing 8 changed files with 897 additions and 653 deletions.
19 changes: 12 additions & 7 deletions projects/json5-parser/grammars/json5.ygg
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,20 @@ entry union Value {
}

class Object {
'{' (ObjectPair (',' ObjectPair)* ',')? '}'
'{' (ObjectPair (COMMA ObjectPair)* COMMA)? '}'
}

class ObjectPair {
ObjectKey COLON Value
}

token {
COLON: ':'
COMMA: ','
}

union ObjectKey {
| Identifier
| String
}

class Array {
'[' (Value (',' Value)* ',')? ']'
'[' (Value (COMMA Value)* COMMA)? ']'
}

atomic class String {
Expand Down Expand Up @@ -71,6 +66,16 @@ class Identifier {
/[_\p{XID_start}][\p{XID_continue}]*/
}


token {
COLON: ':'
COMMA: ','
}

hide class Comment {
| '#' ROL
}

hide class WhiteSpace {
// UNICODE_WHITE_SPACE
' ' | '\n' | '\r'
Expand Down
355 changes: 216 additions & 139 deletions projects/json5-parser/src/codegen/debug.ron

Large diffs are not rendered by default.

36 changes: 28 additions & 8 deletions projects/json5-parser/src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ pub enum Json5Rule {
Value,
Object,
ObjectPair,
COLON,
ObjectKey,
Array,
String,
Expand All @@ -45,22 +44,24 @@ pub enum Json5Rule {
Boolean,
Null,
Identifier,
COLON,
COMMA,
Comment,
WhiteSpace,
/// Label for unnamed text literal
HiddenText,
}

impl YggdrasilRule for Json5Rule {
fn is_ignore(&self) -> bool {
matches!(self, Self::HiddenText | Self::WhiteSpace)
matches!(self, Self::HiddenText | Self::Comment | Self::WhiteSpace)
}

fn get_style(&self) -> &'static str {
match self {
Self::Value => "",
Self::Object => "",
Self::ObjectPair => "",
Self::COLON => "",
Self::ObjectKey => "",
Self::Array => "",
Self::String => "",
Expand All @@ -72,6 +73,9 @@ impl YggdrasilRule for Json5Rule {
Self::Boolean => "",
Self::Null => "",
Self::Identifier => "",
Self::COLON => "",
Self::COMMA => "",
Self::Comment => "",
Self::WhiteSpace => "",
_ => "",
}
Expand All @@ -90,6 +94,7 @@ pub enum ValueNode {
#[derive(Clone, Debug, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ObjectNode {
pub comma: Vec<CommaNode>,
pub object_pair: Vec<ObjectPairNode>,
pub span: Range<usize>,
}
Expand All @@ -103,18 +108,14 @@ pub struct ObjectPairNode {
}
#[derive(Clone, Debug, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ColonNode {
pub span: Range<usize>,
}
#[derive(Clone, Debug, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ObjectKeyNode {
Identifier(IdentifierNode),
String(StringNode),
}
#[derive(Clone, Debug, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ArrayNode {
pub comma: Vec<CommaNode>,
pub value: Vec<ValueNode>,
pub span: Range<usize>,
}
Expand All @@ -134,21 +135,25 @@ pub enum StringElementNode {
#[derive(Clone, Debug, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct HexDigitNode {
pub text: String,
pub span: Range<usize>,
}
#[derive(Clone, Debug, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct EscapedNode {
pub text: String,
pub span: Range<usize>,
}
#[derive(Clone, Debug, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct StringTextNode {
pub text: String,
pub span: Range<usize>,
}
#[derive(Clone, Debug, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct NumberNode {
pub text: String,
pub span: Range<usize>,
}
#[derive(Clone, Debug, Hash)]
Expand All @@ -169,6 +174,21 @@ pub struct IdentifierNode {
}
#[derive(Clone, Debug, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ColonNode {
pub span: Range<usize>,
}
#[derive(Clone, Debug, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CommaNode {
pub span: Range<usize>,
}
#[derive(Clone, Debug, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CommentNode {
pub span: Range<usize>,
}
#[derive(Clone, Debug, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct WhiteSpaceNode {
pub span: Range<usize>,
}
90 changes: 66 additions & 24 deletions projects/json5-parser/src/codegen/parse_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl YggdrasilNode for ObjectNode {
fn from_pair(pair: TokenPair<Self::Rule>) -> Result<Self, YggdrasilError<Self::Rule>> {
let _span = pair.get_span();
Ok(Self {
comma: pair.take_tagged_items::<CommaNode>(Cow::Borrowed("comma")).collect::<Result<Vec<_>, _>>()?,
object_pair: pair
.take_tagged_items::<ObjectPairNode>(Cow::Borrowed("object_pair"))
.collect::<Result<Vec<_>, _>>()?,
Expand Down Expand Up @@ -95,26 +96,6 @@ impl FromStr for ObjectPairNode {
}
}
#[automatically_derived]
impl YggdrasilNode for ColonNode {
type Rule = Json5Rule;

fn get_range(&self) -> Range<usize> {
Range { start: self.span.start as usize, end: self.span.end as usize }
}
fn from_pair(pair: TokenPair<Self::Rule>) -> Result<Self, YggdrasilError<Self::Rule>> {
let _span = pair.get_span();
Ok(Self { span: Range { start: _span.start() as usize, end: _span.end() as usize } })
}
}
#[automatically_derived]
impl FromStr for ColonNode {
type Err = YggdrasilError<Json5Rule>;

fn from_str(input: &str) -> Result<Self, YggdrasilError<Json5Rule>> {
Self::from_cst(Json5Parser::parse_cst(input, Json5Rule::COLON)?)
}
}
#[automatically_derived]
impl YggdrasilNode for ObjectKeyNode {
type Rule = Json5Rule;

Expand Down Expand Up @@ -153,6 +134,7 @@ impl YggdrasilNode for ArrayNode {
fn from_pair(pair: TokenPair<Self::Rule>) -> Result<Self, YggdrasilError<Self::Rule>> {
let _span = pair.get_span();
Ok(Self {
comma: pair.take_tagged_items::<CommaNode>(Cow::Borrowed("comma")).collect::<Result<Vec<_>, _>>()?,
value: pair.take_tagged_items::<ValueNode>(Cow::Borrowed("value")).collect::<Result<Vec<_>, _>>()?,
span: Range { start: _span.start() as usize, end: _span.end() as usize },
})
Expand Down Expand Up @@ -233,7 +215,7 @@ impl YggdrasilNode for HexDigitNode {
}
fn from_pair(pair: TokenPair<Self::Rule>) -> Result<Self, YggdrasilError<Self::Rule>> {
let _span = pair.get_span();
Ok(Self { span: Range { start: _span.start() as usize, end: _span.end() as usize } })
Ok(Self { text: pair.get_string(), span: Range { start: _span.start() as usize, end: _span.end() as usize } })
}
}
#[automatically_derived]
Expand All @@ -253,7 +235,7 @@ impl YggdrasilNode for EscapedNode {
}
fn from_pair(pair: TokenPair<Self::Rule>) -> Result<Self, YggdrasilError<Self::Rule>> {
let _span = pair.get_span();
Ok(Self { span: Range { start: _span.start() as usize, end: _span.end() as usize } })
Ok(Self { text: pair.get_string(), span: Range { start: _span.start() as usize, end: _span.end() as usize } })
}
}
#[automatically_derived]
Expand All @@ -273,7 +255,7 @@ impl YggdrasilNode for StringTextNode {
}
fn from_pair(pair: TokenPair<Self::Rule>) -> Result<Self, YggdrasilError<Self::Rule>> {
let _span = pair.get_span();
Ok(Self { span: Range { start: _span.start() as usize, end: _span.end() as usize } })
Ok(Self { text: pair.get_string(), span: Range { start: _span.start() as usize, end: _span.end() as usize } })
}
}
#[automatically_derived]
Expand All @@ -293,7 +275,7 @@ impl YggdrasilNode for NumberNode {
}
fn from_pair(pair: TokenPair<Self::Rule>) -> Result<Self, YggdrasilError<Self::Rule>> {
let _span = pair.get_span();
Ok(Self { span: Range { start: _span.start() as usize, end: _span.end() as usize } })
Ok(Self { text: pair.get_string(), span: Range { start: _span.start() as usize, end: _span.end() as usize } })
}
}
#[automatically_derived]
Expand Down Expand Up @@ -374,6 +356,66 @@ impl FromStr for IdentifierNode {
}
}
#[automatically_derived]
impl YggdrasilNode for ColonNode {
type Rule = Json5Rule;

fn get_range(&self) -> Range<usize> {
Range { start: self.span.start as usize, end: self.span.end as usize }
}
fn from_pair(pair: TokenPair<Self::Rule>) -> Result<Self, YggdrasilError<Self::Rule>> {
let _span = pair.get_span();
Ok(Self { span: Range { start: _span.start() as usize, end: _span.end() as usize } })
}
}
#[automatically_derived]
impl FromStr for ColonNode {
type Err = YggdrasilError<Json5Rule>;

fn from_str(input: &str) -> Result<Self, YggdrasilError<Json5Rule>> {
Self::from_cst(Json5Parser::parse_cst(input, Json5Rule::COLON)?)
}
}
#[automatically_derived]
impl YggdrasilNode for CommaNode {
type Rule = Json5Rule;

fn get_range(&self) -> Range<usize> {
Range { start: self.span.start as usize, end: self.span.end as usize }
}
fn from_pair(pair: TokenPair<Self::Rule>) -> Result<Self, YggdrasilError<Self::Rule>> {
let _span = pair.get_span();
Ok(Self { span: Range { start: _span.start() as usize, end: _span.end() as usize } })
}
}
#[automatically_derived]
impl FromStr for CommaNode {
type Err = YggdrasilError<Json5Rule>;

fn from_str(input: &str) -> Result<Self, YggdrasilError<Json5Rule>> {
Self::from_cst(Json5Parser::parse_cst(input, Json5Rule::COMMA)?)
}
}
#[automatically_derived]
impl YggdrasilNode for CommentNode {
type Rule = Json5Rule;

fn get_range(&self) -> Range<usize> {
Range { start: self.span.start as usize, end: self.span.end as usize }
}
fn from_pair(pair: TokenPair<Self::Rule>) -> Result<Self, YggdrasilError<Self::Rule>> {
let _span = pair.get_span();
Ok(Self { span: Range { start: _span.start() as usize, end: _span.end() as usize } })
}
}
#[automatically_derived]
impl FromStr for CommentNode {
type Err = YggdrasilError<Json5Rule>;

fn from_str(input: &str) -> Result<Self, YggdrasilError<Json5Rule>> {
Self::from_cst(Json5Parser::parse_cst(input, Json5Rule::Comment)?)
}
}
#[automatically_derived]
impl YggdrasilNode for WhiteSpaceNode {
type Rule = Json5Rule;

Expand Down
Loading

0 comments on commit 6443781

Please sign in to comment.