diff options
Diffstat (limited to 'src/syntax')
| -rw-r--r-- | src/syntax/color.rs | 3 | ||||
| -rw-r--r-- | src/syntax/expr.rs | 4 | ||||
| -rw-r--r-- | src/syntax/mod.rs | 113 | ||||
| -rw-r--r-- | src/syntax/parsing.rs | 27 | ||||
| -rw-r--r-- | src/syntax/span.rs | 18 |
5 files changed, 98 insertions, 67 deletions
diff --git a/src/syntax/color.rs b/src/syntax/color.rs deleted file mode 100644 index 65525480..00000000 --- a/src/syntax/color.rs +++ /dev/null @@ -1,3 +0,0 @@ -use super::*; - - diff --git a/src/syntax/expr.rs b/src/syntax/expr.rs index b06b29c8..c4feea74 100644 --- a/src/syntax/expr.rs +++ b/src/syntax/expr.rs @@ -107,6 +107,10 @@ impl Object { impl Display for Object { fn fmt(&self, f: &mut Formatter) -> fmt::Result { + if self.pairs.len() == 0 { + return write!(f, "{{}}"); + } + write!(f, "{{ ")?; let mut first = true; diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs index 1c72de4d..bcec05af 100644 --- a/src/syntax/mod.rs +++ b/src/syntax/mod.rs @@ -9,7 +9,6 @@ use crate::size::{Size, ScaleSize}; pub type ParseResult<T> = crate::TypesetResult<T>; -pub_use_mod!(color); pub_use_mod!(expr); pub_use_mod!(tokens); pub_use_mod!(parsing); @@ -93,7 +92,7 @@ impl SyntaxTree { } /// A node in the syntax tree. -#[derive(Debug, PartialEq)] +#[derive(PartialEq)] pub enum Node { /// A number of whitespace characters containing less than two newlines. Space, @@ -111,6 +110,28 @@ pub enum Node { Func(FuncCall), } +impl Display for Node { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + match self { + Node::Space => write!(f, "Space"), + Node::Newline => write!(f, "Newline"), + Node::Text(text) => write!(f, "{:?}", text), + Node::ToggleItalic => write!(f, "ToggleItalic"), + Node::ToggleBolder => write!(f, "ToggleBold"), + Node::ToggleMonospace => write!(f, "ToggleMonospace"), + Node::Func(func) => { + if f.alternate() { + write!(f, "{:#?}", func.0) + } else { + write!(f, "{:?}", func.0) + } + } + } + } +} + +debug_display!(Node); + /// An invocation of a function. #[derive(Debug)] pub struct FuncCall(pub Box<dyn LayoutFunc>); @@ -121,59 +142,20 @@ impl PartialEq for FuncCall { } } -#[derive(Debug, Clone, Eq, PartialEq)] -pub struct Colorization { - pub colors: Vec<Spanned<ColorToken>>, -} - -/// Entities which can be colored by syntax highlighting. -#[derive(Debug, Copy, Clone, Eq, PartialEq)] -pub enum ColorToken { - Comment, - - Bracket, - FuncName, - Colon, - - Key, - Equals, - Comma, - - Paren, - Brace, - - ExprIdent, - ExprStr, - ExprNumber, - ExprSize, - ExprBool, - - Bold, - Italic, - Monospace, - - Invalid, -} - -#[derive(Debug, Clone, Eq, PartialEq)] -pub struct ErrorMap { - pub errors: Vec<Spanned<String>>, -} - -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq)] pub struct FuncHeader { pub name: Spanned<Ident>, pub args: FuncArgs, } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq)] pub struct FuncArgs { - positional: Tuple, - keyword: Object, + pub positional: Tuple, + pub keyword: Object, } impl FuncArgs { - fn new() -> FuncArgs { + pub fn new() -> FuncArgs { FuncArgs { positional: Tuple::new(), keyword: Object::new(), @@ -258,3 +240,42 @@ fn expect<E: ExpressionKind>(opt: ParseResult<Option<E>>) -> ParseResult<E> { Err(e) => Err(e), } } + +#[derive(Debug, Clone, Eq, PartialEq)] +pub struct Colorization { + pub tokens: Vec<Spanned<ColorToken>>, +} + +/// Entities which can be colored by syntax highlighting. +#[derive(Debug, Copy, Clone, Eq, PartialEq)] +pub enum ColorToken { + Comment, + + Bracket, + FuncName, + Colon, + + Key, + Equals, + Comma, + + Paren, + Brace, + + ExprIdent, + ExprStr, + ExprNumber, + ExprSize, + ExprBool, + + Bold, + Italic, + Monospace, + + Invalid, +} + +#[derive(Debug, Clone, Eq, PartialEq)] +pub struct ErrorMap { + pub errors: Vec<Spanned<String>>, +} diff --git a/src/syntax/parsing.rs b/src/syntax/parsing.rs index bf3bea89..f0a68641 100644 --- a/src/syntax/parsing.rs +++ b/src/syntax/parsing.rs @@ -33,7 +33,7 @@ impl<'s> Parser<'s> { src, ctx, error_map: ErrorMap { errors: vec![] }, - colorization: Colorization { colors: vec![] }, + colorization: Colorization { tokens: vec![] }, tokens: Tokens::new(src), peeked: None, @@ -114,8 +114,6 @@ impl<'s> Parser<'s> { } fn parse_func_call(&mut self, header: Option<FuncHeader>) -> Option<FuncCall> { - println!("peek: {:?}", self.peek()); - let body = if self.peek() == Some(LeftBracket) { self.eat(); @@ -140,13 +138,15 @@ impl<'s> Parser<'s> { }; let header = header?; - let name = header.name; - let parser = self.ctx.scope.get_parser(name.v.as_str()).or_else(|| { - self.error(format!("unknown function: `{}`", name.v), name.span); + let parser = self.ctx.scope.get_parser(header.name.v.as_str()).or_else(|| { + self.error( + format!("unknown function: `{}`", header.name.v), + header.name.span + ); None })?; - Some(FuncCall(parser(header.args, body, self.ctx).unwrap())) + Some(FuncCall(parser(header, body, self.ctx).unwrap())) } fn parse_func_name(&mut self) -> Option<Spanned<Ident>> { @@ -163,16 +163,17 @@ impl<'s> Parser<'s> { } fn parse_func_args(&mut self) -> FuncArgs { - // unimplemented!() + // todo!() + self.eat_until(|t| t == RightBracket, true); FuncArgs::new() } fn parse_tuple(&mut self) -> Spanned<Expression> { - unimplemented!("parse_tuple") + todo!("parse_tuple") } fn parse_object(&mut self) -> Spanned<Expression> { - unimplemented!("parse_object") + todo!("parse_object") } fn skip_whitespace(&mut self) { @@ -207,13 +208,13 @@ impl<'s> Parser<'s> { fn color(&mut self, token: Spanned<ColorToken>, replace_last: bool) { if replace_last { - if let Some(last) = self.colorization.colors.last_mut() { + if let Some(last) = self.colorization.tokens.last_mut() { *last = token; return; } } - self.colorization.colors.push(token); + self.colorization.tokens.push(token); } fn color_token(&mut self, token: Spanned<Token<'s>>) { @@ -235,7 +236,7 @@ impl<'s> Parser<'s> { }; if let Some(color) = colored { - self.colorization.colors.push(Spanned { v: color, span: token.span }); + self.colorization.tokens.push(Spanned { v: color, span: token.span }); } } diff --git a/src/syntax/span.rs b/src/syntax/span.rs index e5c6912b..546b3ad6 100644 --- a/src/syntax/span.rs +++ b/src/syntax/span.rs @@ -1,6 +1,6 @@ //! Spans map elements to the part of source code they originate from. -use std::fmt::{self, Display, Formatter}; +use std::fmt::{self, Debug, Display, Formatter}; /// Annotates a value with the part of the source code it corresponds to. @@ -28,13 +28,21 @@ impl<T> Spanned<T> { } } -impl<T> Display for Spanned<T> where T: std::fmt::Debug { +impl<T> Display for Spanned<T> where T: std::fmt::Display { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "({:?}:{})", self.v, self.span) + write!(f, "({}, {}, ", self.span.start, self.span.end)?; + self.v.fmt(f)?; + write!(f, ")") } } -debug_display!(Spanned; T where T: std::fmt::Debug); +impl<T> Debug for Spanned<T> where T: std::fmt::Debug { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + write!(f, "({}, {}, ", self.span.start, self.span.end)?; + self.v.fmt(f)?; + write!(f, ")") + } +} /// Describes a slice of source code. #[derive(Copy, Clone, Eq, PartialEq, Hash)] @@ -68,7 +76,7 @@ impl Span { impl Display for Span { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "[{}, {}]", self.start, self.end) + write!(f, "({}, {})", self.start, self.end) } } |
