Skip to content

Commit

Permalink
It works
Browse files Browse the repository at this point in the history
  • Loading branch information
wycats committed Oct 10, 2019
1 parent cf7e364 commit 463df69
Show file tree
Hide file tree
Showing 12 changed files with 355 additions and 253 deletions.
118 changes: 81 additions & 37 deletions src/parser/hir/syntax_shape.rs
Expand Up @@ -15,7 +15,7 @@ use crate::parser::PipelineElement;
use crate::parser::{
hir,
hir::{debug_tokens, TokensIterator},
FlatShape, Operator, Pipeline, RawToken, TokenNode,
Operator, Pipeline, RawToken, TokenNode,
};
use crate::prelude::*;
use derive_new::new;
Expand All @@ -39,6 +39,7 @@ pub(crate) use self::expression::variable_path::{
ExpressionContinuationShape, MemberShape, PathTailShape, VariablePathShape,
};
pub(crate) use self::expression::{continue_expression, AnyExpressionShape};
pub(crate) use self::flat_shape::FlatShape;

#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub enum SyntaxShape {
Expand Down Expand Up @@ -66,30 +67,39 @@ impl FallibleColorSyntax for SyntaxShape {
shapes: &mut Vec<Tagged<FlatShape>>,
) -> Result<(), ShellError> {
match self {
SyntaxShape::Any => color_syntax(&AnyExpressionShape, token_nodes, context, shapes).1,
SyntaxShape::Any => {
color_fallible_syntax(&AnyExpressionShape, token_nodes, context, shapes)
}
SyntaxShape::List => {
color_syntax(&ExpressionListShape, token_nodes, context, shapes);
Ok(())
}
SyntaxShape::Int => color_syntax(&IntShape, token_nodes, context, shapes).1,
SyntaxShape::String => {
color_syntax_with(
&StringShape,
&FlatShape::String,
token_nodes,
context,
shapes,
)
.1
SyntaxShape::Int => color_fallible_syntax(&IntShape, token_nodes, context, shapes),
SyntaxShape::String => color_fallible_syntax_with(
&StringShape,
&FlatShape::String,
token_nodes,
context,
shapes,
),
SyntaxShape::Member => {
color_fallible_syntax(&MemberShape, token_nodes, context, shapes)
}
SyntaxShape::Member => color_syntax(&MemberShape, token_nodes, context, shapes).1,
SyntaxShape::ColumnPath => {
color_syntax(&ColumnPathShape, token_nodes, context, shapes).1
color_fallible_syntax(&ColumnPathShape, token_nodes, context, shapes)
}
SyntaxShape::Number => {
color_fallible_syntax(&NumberShape, token_nodes, context, shapes)
}
SyntaxShape::Path => {
color_fallible_syntax(&FilePathShape, token_nodes, context, shapes)
}
SyntaxShape::Pattern => {
color_fallible_syntax(&PatternShape, token_nodes, context, shapes)
}
SyntaxShape::Block => {
color_fallible_syntax(&AnyBlockShape, token_nodes, context, shapes)
}
SyntaxShape::Number => color_syntax(&NumberShape, token_nodes, context, shapes).1,
SyntaxShape::Path => color_syntax(&FilePathShape, token_nodes, context, shapes).1,
SyntaxShape::Pattern => color_syntax(&PatternShape, token_nodes, context, shapes).1,
SyntaxShape::Block => color_syntax(&AnyBlockShape, token_nodes, context, shapes).1,
}
}
}
Expand Down Expand Up @@ -218,23 +228,23 @@ pub trait ColorSyntax: std::fmt::Debug + Copy {
) -> Self::Info;
}

impl<T> ColorSyntax for T
where
T: FallibleColorSyntax,
{
type Info = Result<T::Info, ShellError>;
type Input = T::Input;

fn color_syntax<'a, 'b>(
&self,
input: &Self::Input,
token_nodes: &'b mut TokensIterator<'a>,
context: &ExpandContext,
shapes: &mut Vec<Tagged<FlatShape>>,
) -> Result<T::Info, ShellError> {
FallibleColorSyntax::color_syntax(self, input, token_nodes, context, shapes)
}
}
// impl<T> ColorSyntax for T
// where
// T: FallibleColorSyntax,
// {
// type Info = Result<T::Info, ShellError>;
// type Input = T::Input;

// fn color_syntax<'a, 'b>(
// &self,
// input: &Self::Input,
// token_nodes: &'b mut TokensIterator<'a>,
// context: &ExpandContext,
// shapes: &mut Vec<Tagged<FlatShape>>,
// ) -> Result<T::Info, ShellError> {
// FallibleColorSyntax::color_syntax(self, input, token_nodes, context, shapes)
// }
// }

pub(crate) trait ExpandSyntax: std::fmt::Debug + Copy {
type Output: std::fmt::Debug;
Expand Down Expand Up @@ -358,6 +368,40 @@ pub fn color_syntax_with<'a, 'b, T: ColorSyntax<Info = U, Input = I>, U, I>(
((), result)
}

pub fn color_fallible_syntax_with<'a, 'b, T: FallibleColorSyntax<Info = U, Input = I>, U, I>(
shape: &T,
input: &I,
token_nodes: &'b mut TokensIterator<'a>,
context: &ExpandContext,
shapes: &mut Vec<Tagged<FlatShape>>,
) -> Result<U, ShellError> {
trace!(target: "nu::color_syntax", "before {} :: {:?}", std::any::type_name::<T>(), debug_tokens(token_nodes, context.source));

if token_nodes.at_end() {
trace!(target: "nu::color_syntax", "at eof");
return Err(ShellError::unexpected_eof("coloring", Tag::unknown()));
}

let len = shapes.len();
let result = shape.color_syntax(input, token_nodes, context, shapes);

trace!(target: "nu::color_syntax", "ok :: {:?}", debug_tokens(token_nodes, context.source));

if log_enabled!(target: "nu::color_syntax", log::Level::Trace) {
trace!(target: "nu::color_syntax", "after {}", std::any::type_name::<T>());

if len < shapes.len() {
for i in len..(shapes.len()) {
trace!(target: "nu::color_syntax", "new shape :: {:?}", shapes[i]);
}
} else {
trace!(target: "nu::color_syntax", "no new shapes");
}
}

result
}

pub(crate) fn expand_expr<'a, 'b, T: ExpandExpression>(
shape: &T,
token_nodes: &'b mut TokensIterator<'a>,
Expand Down Expand Up @@ -670,7 +714,7 @@ impl FallibleColorSyntax for CommandHeadShape {
shapes: &mut Vec<Tagged<FlatShape>>,
) -> Result<CommandHeadKind, ShellError> {
// If we don't ultimately find a token, roll back
token_nodes.checkpoint_with(|token_nodes| {
token_nodes.atomic(|token_nodes| {
// First, take a look at the next token
let atom = expand_atom(
token_nodes,
Expand Down Expand Up @@ -1197,7 +1241,7 @@ impl ColorSyntax for CommandShape {
context: &ExpandContext,
shapes: &mut Vec<Tagged<FlatShape>>,
) {
let (_, kind) = color_syntax(&CommandHeadShape, token_nodes, context, shapes);
let kind = color_fallible_syntax(&CommandHeadShape, token_nodes, context, shapes);

match kind {
Err(_) => {
Expand Down
18 changes: 9 additions & 9 deletions src/parser/hir/syntax_shape/block.rs
Expand Up @@ -2,10 +2,10 @@ use crate::errors::ShellError;
use crate::parser::{
hir,
hir::syntax_shape::{
color_fallible_syntax, color_syntax, color_syntax_with, continue_expression, expand_expr,
expand_syntax, DelimitedShape, ExpandContext, ExpandExpression,
ExpressionContinuationShape, ExpressionListShape, FallibleColorSyntax, FlatShape,
MemberShape, PathTailShape, VariablePathShape,
color_fallible_syntax, color_syntax_with, continue_expression, expand_expr, expand_syntax,
DelimitedShape, ExpandContext, ExpandExpression, ExpressionContinuationShape,
ExpressionListShape, FallibleColorSyntax, FlatShape, MemberShape, PathTailShape,
VariablePathShape,
},
hir::tokens_iterator::TokensIterator,
parse::token_tree::Delimiter,
Expand Down Expand Up @@ -55,7 +55,7 @@ impl FallibleColorSyntax for AnyBlockShape {
}

// Otherwise, look for a shorthand block. If none found, fail
color_syntax(&ShorthandBlock, token_nodes, context, shapes).1
color_fallible_syntax(&ShorthandBlock, token_nodes, context, shapes)
}
}

Expand Down Expand Up @@ -150,8 +150,8 @@ impl FallibleColorSyntax for ShorthandPath {
context: &ExpandContext,
shapes: &mut Vec<Tagged<FlatShape>>,
) -> Result<(), ShellError> {
token_nodes.checkpoint_with(|token_nodes| {
let variable = color_syntax(&VariablePathShape, token_nodes, context, shapes).1;
token_nodes.atomic(|token_nodes| {
let variable = color_fallible_syntax(&VariablePathShape, token_nodes, context, shapes);

match variable {
Ok(_) => {
Expand All @@ -165,11 +165,11 @@ impl FallibleColorSyntax for ShorthandPath {
}

// look for a member (`<member>` -> `$it.<member>`)
color_syntax(&MemberShape, token_nodes, context, shapes).1?;
color_fallible_syntax(&MemberShape, token_nodes, context, shapes)?;

// Now that we've synthesized the head, of the path, proceed to expand the tail of the path
// like any other path.
let tail = color_syntax(&PathTailShape, token_nodes, context, shapes).1;
let tail = color_fallible_syntax(&PathTailShape, token_nodes, context, shapes);

match tail {
Ok(_) => {}
Expand Down
27 changes: 16 additions & 11 deletions src/parser/hir/syntax_shape/expression.rs
Expand Up @@ -9,9 +9,9 @@ pub(crate) mod unit;
pub(crate) mod variable_path;

use crate::parser::hir::syntax_shape::{
color_delimited_square, color_syntax, color_syntax_with, expand_atom, expand_delimited_square,
expand_expr, expand_syntax, AtomicToken, BareShape, ColorableDotShape, DotShape, ExpandContext,
ExpandExpression, ExpandSyntax, ExpansionRule, ExpressionContinuation,
color_delimited_square, color_fallible_syntax, color_fallible_syntax_with, expand_atom,
expand_delimited_square, expand_expr, expand_syntax, AtomicToken, BareShape, ColorableDotShape,
DotShape, ExpandContext, ExpandExpression, ExpandSyntax, ExpansionRule, ExpressionContinuation,
ExpressionContinuationShape, FallibleColorSyntax, FlatShape,
};
use crate::parser::{
Expand Down Expand Up @@ -49,7 +49,7 @@ impl FallibleColorSyntax for AnyExpressionShape {
shapes: &mut Vec<Tagged<FlatShape>>,
) -> Result<(), ShellError> {
// Look for an expression at the cursor
color_syntax(&AnyExpressionStartShape, token_nodes, context, shapes).1?;
color_fallible_syntax(&AnyExpressionStartShape, token_nodes, context, shapes)?;

match continue_coloring_expression(token_nodes, context, shapes) {
Err(_) => {
Expand Down Expand Up @@ -97,11 +97,12 @@ pub(crate) fn continue_coloring_expression(
shapes: &mut Vec<Tagged<FlatShape>>,
) -> Result<(), ShellError> {
// if there's not even one expression continuation, fail
color_syntax(&ExpressionContinuationShape, token_nodes, context, shapes).1?;
color_fallible_syntax(&ExpressionContinuationShape, token_nodes, context, shapes)?;

loop {
// Check to see whether there's any continuation after the head expression
let result = color_syntax(&ExpressionContinuationShape, token_nodes, context, shapes).1;
let result =
color_fallible_syntax(&ExpressionContinuationShape, token_nodes, context, shapes);

match result {
Err(_) => {
Expand Down Expand Up @@ -223,8 +224,13 @@ impl FallibleColorSyntax for BareTailShape {
let len = shapes.len();

loop {
let word =
color_syntax_with(&BareShape, &FlatShape::Word, token_nodes, context, shapes).1;
let word = color_fallible_syntax_with(
&BareShape,
&FlatShape::Word,
token_nodes,
context,
shapes,
);

match word {
// if a word was found, continue
Expand All @@ -234,14 +240,13 @@ impl FallibleColorSyntax for BareTailShape {
}

// try to find a dot
let dot = color_syntax_with(
let dot = color_fallible_syntax_with(
&ColorableDotShape,
&FlatShape::Word,
token_nodes,
context,
shapes,
)
.1;
);

match dot {
// if a dot was found, try to find another word
Expand Down

0 comments on commit 463df69

Please sign in to comment.