diff options
| author | Laurenz <laurmaedje@gmail.com> | 2020-01-18 15:58:36 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2020-01-18 15:58:36 +0100 |
| commit | 277f2d2176f5e98305870f90b16af3feae1bb3d1 (patch) | |
| tree | a267216f67ebed87cbf83f021bfb01e9b79feae4 /src/syntax | |
| parent | 70878885f5d169f2c5d9e66d3919ee56d5f9f9ca (diff) | |
ColorToken serialization + function fix 🚧
Diffstat (limited to 'src/syntax')
| -rw-r--r-- | src/syntax/mod.rs | 8 | ||||
| -rw-r--r-- | src/syntax/parsing.rs | 12 | ||||
| -rw-r--r-- | src/syntax/span.rs | 7 |
3 files changed, 17 insertions, 10 deletions
diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs index e9725f04..f644f051 100644 --- a/src/syntax/mod.rs +++ b/src/syntax/mod.rs @@ -2,6 +2,7 @@ use std::fmt::{self, Display, Formatter}; use unicode_xid::UnicodeXID; +use serde::Serialize; use crate::func::LayoutFunc; use crate::size::{Size, ScaleSize}; @@ -257,7 +258,7 @@ fn expect<E: ExpressionKind>(opt: ParseResult<Option<E>>) -> ParseResult<E> { } } -#[derive(Debug, Clone, Eq, PartialEq)] +#[derive(Debug, Clone, Eq, PartialEq, Serialize)] pub struct Colorization { pub tokens: Vec<Spanned<ColorToken>>, } @@ -277,7 +278,8 @@ impl Colorization { } /// Entities which can be colored by syntax highlighting. -#[derive(Debug, Copy, Clone, Eq, PartialEq)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize)] +#[serde(rename_all = "camelCase")] pub enum ColorToken { Comment, Bracket, @@ -299,7 +301,7 @@ pub enum ColorToken { Invalid, } -#[derive(Debug, Clone, Eq, PartialEq)] +#[derive(Debug, Clone, Eq, PartialEq, Serialize)] pub struct ErrorMap { pub errors: Vec<Spanned<String>>, } diff --git a/src/syntax/parsing.rs b/src/syntax/parsing.rs index 71d6b251..f6d0b629 100644 --- a/src/syntax/parsing.rs +++ b/src/syntax/parsing.rs @@ -104,8 +104,11 @@ impl<'s> Parser<'s> { let name = self.parse_func_name()?; self.skip_whitespace(); - let args = match self.eat() { - Some(Spanned { v: Colon, .. }) => self.parse_func_args(), + let args = match self.peek() { + Some(Spanned { v: Colon, .. }) => { + self.eat(); + self.parse_func_args() + } Some(Spanned { v: RightBracket, .. }) => FuncArgs::new(), other => { self.expected_at("colon or closing bracket", name.span.end); @@ -119,8 +122,9 @@ impl<'s> Parser<'s> { /// Parses the function name if is the next token. Otherwise, it adds an /// error and returns `None`. fn parse_func_name(&mut self) -> Option<Spanned<Ident>> { - match self.eat() { + match self.peek() { Some(Spanned { v: ExprIdent(ident), span }) => { + self.eat(); self.colorization.replace_last(ColorToken::FuncName); return Some(Spanned { v: Ident(ident.to_string()), span }); } @@ -280,7 +284,7 @@ impl<'s> Parser<'s> { None })?; - Some(FuncCall(parser(header, body, self.ctx).unwrap())) + parser(header, body, self.ctx).ok().map(|f| FuncCall(f)) } /// Skip all whitespace/comment tokens. diff --git a/src/syntax/span.rs b/src/syntax/span.rs index 546b3ad6..df9a3520 100644 --- a/src/syntax/span.rs +++ b/src/syntax/span.rs @@ -1,10 +1,11 @@ //! Spans map elements to the part of source code they originate from. use std::fmt::{self, Debug, Display, Formatter}; +use serde::Serialize; /// Annotates a value with the part of the source code it corresponds to. -#[derive(Copy, Clone, Eq, PartialEq, Hash)] +#[derive(Copy, Clone, Eq, PartialEq, Hash, Serialize)] pub struct Spanned<T> { pub v: T, pub span: Span, @@ -45,7 +46,7 @@ impl<T> Debug for Spanned<T> where T: std::fmt::Debug { } /// Describes a slice of source code. -#[derive(Copy, Clone, Eq, PartialEq, Hash)] +#[derive(Copy, Clone, Eq, PartialEq, Hash, Serialize)] pub struct Span { pub start: Position, pub end: Position, @@ -83,7 +84,7 @@ impl Display for Span { debug_display!(Span); /// A line-column position in source code. -#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] +#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize)] pub struct Position { /// The 0-indexed line (inclusive). pub line: usize, |
