drive let from internal call

This commit is contained in:
JT 2021-07-08 18:19:38 +12:00
parent 04cbef3aa8
commit 5d4ae4a2a4
3 changed files with 270 additions and 157 deletions

View file

@ -22,6 +22,16 @@ fn main() -> std::io::Result<()> {
.required("else_block", SyntaxShape::Block, "else block");
working_set.add_decl((b"if").to_vec(), sig);
let sig = Signature::build("let")
.required("var_name", SyntaxShape::Variable, "variable name")
.required("=", SyntaxShape::Literal(b"=".to_vec()), "equals sign")
.required(
"value",
SyntaxShape::Expression,
"the value to set the variable to",
);
working_set.add_decl((b"let").to_vec(), sig);
//let file = std::fs::read(&path)?;
//let (output, err) = working_set.parse_file(&path, file);
let (output, err) = working_set.parse_source(path.as_bytes());

View file

@ -17,4 +17,5 @@ pub enum ParseError {
MissingPositional(String, Span),
MissingRequiredFlag(String, Span),
IncompleteMathExpression(Span),
UnknownState(String, Span),
}

View file

@ -3,7 +3,7 @@ use std::ops::{Index, IndexMut};
use crate::{
lex, lite_parse,
parser_state::{Type, VarId},
span, DeclId, LiteBlock, ParseError, ParserWorkingSet, Signature, Span,
DeclId, LiteBlock, ParseError, ParserWorkingSet, Signature, Span,
};
/// The syntactic shapes that values must match to be passed into a command. You can think of this as the type-checking that occurs when you call a function.
@ -61,6 +61,9 @@ pub enum SyntaxShape {
/// A general math expression, eg `1 + 2`
MathExpression,
/// A variable name
Variable,
/// A general expression, eg `1 + 2` or `foo --bar`
Expression,
}
@ -300,13 +303,13 @@ impl ParserWorkingSet {
(Expression::garbage(spans[0]), None)
}
pub fn parse_call(&mut self, spans: &[Span]) -> (Expression, Option<ParseError>) {
pub fn parse_internal_call(
&mut self,
spans: &[Span],
decl_id: usize,
) -> (Box<Call>, Span, Option<ParseError>) {
let mut error = None;
// assume spans.len() > 0?
let name = self.get_span_contents(spans[0]);
if let Some(decl_id) = self.find_decl(name) {
let mut call = Call::new();
call.decl_id = decl_id;
@ -366,9 +369,8 @@ impl ParserWorkingSet {
if let Some(flag) = sig.get_short_flag(short_flag_char) {
// If we require an arg and are in a batch of short flags, error
if !found_short_flags.is_empty() && flag.arg.is_some() {
error = error.or(Some(ParseError::ShortFlagBatchCantTakeArg(
short_flag_span,
)))
error = error
.or(Some(ParseError::ShortFlagBatchCantTakeArg(short_flag_span)))
}
found_short_flags.push(flag);
} else {
@ -442,6 +444,26 @@ impl ParserWorkingSet {
arg_offset = spans.len() - remainder;
}
}
SyntaxShape::Expression => {
let remainder = sig.num_positionals() - positional_idx;
if spans.len() < remainder {
error = error.or_else(|| {
Some(ParseError::MissingPositional(
"required args".into(),
arg_span,
))
});
} else {
let (arg, err) = self.parse_expression(
&spans[arg_offset..(spans.len() - remainder + 1)],
);
error = error.or(err);
call.positional.push(arg);
arg_offset = spans.len() - remainder;
}
}
SyntaxShape::Literal(literal) => {
if arg_contents != literal {
// When keywords mismatch, this is a strong indicator of something going wrong.
@ -449,7 +471,7 @@ impl ParserWorkingSet {
// go ahead and override the current error and tell the user about the missing
// keyword/literal.
error = Some(ParseError::Mismatch(
format!("{}", String::from_utf8_lossy(&literal)),
String::from_utf8_lossy(&literal).into(),
arg_span,
))
}
@ -476,13 +498,21 @@ impl ParserWorkingSet {
error = error.or(err);
// FIXME: type unknown
(Box::new(call), span(spans), error)
}
pub fn parse_call(&mut self, spans: &[Span]) -> (Expression, Option<ParseError>) {
// assume spans.len() > 0?
let name = self.get_span_contents(spans[0]);
if let Some(decl_id) = self.find_decl(name) {
let (call, span, err) = self.parse_internal_call(spans, decl_id);
(
Expression {
expr: Expr::Call(Box::new(call)),
//ty: Type::Unknown,
span: span(spans),
expr: Expr::Call(call),
span,
},
error,
err,
)
} else {
self.parse_external_call(spans)
@ -578,6 +608,36 @@ impl ParserWorkingSet {
}
}
pub fn parse_variable_expr(&mut self, span: Span) -> (Expression, Option<ParseError>) {
let (id, err) = self.parse_variable(span);
if err.is_none() {
if let Some(id) = id {
(
Expression {
expr: Expr::Var(id),
span,
},
None,
)
} else {
let name = self.get_span_contents(span).to_vec();
// this seems okay to set it to unknown here, but we should double-check
let id = self.add_variable(name, Type::Unknown);
(
Expression {
expr: Expr::Var(id),
span,
},
None,
)
}
} else {
(garbage(span), err)
}
}
pub fn parse_full_column_path(&mut self, span: Span) -> (Expression, Option<ParseError>) {
// FIXME: assume for now a paren expr, but needs more
let bytes = self.get_span_contents(span);
@ -809,7 +869,9 @@ impl ParserWorkingSet {
shape: SyntaxShape,
) -> (Expression, Option<ParseError>) {
let bytes = self.get_span_contents(span);
if bytes.starts_with(b"$") {
if shape == SyntaxShape::Variable {
return self.parse_variable_expr(span);
} else if bytes.starts_with(b"$") {
return self.parse_dollar_expr(span);
} else if bytes.starts_with(b"(") {
return self.parse_full_column_path(span);
@ -873,7 +935,9 @@ impl ParserWorkingSet {
)
}
}
SyntaxShape::String => self.parse_string(span),
SyntaxShape::String | SyntaxShape::GlobPattern | SyntaxShape::FilePath => {
self.parse_string(span)
}
SyntaxShape::Block => self.parse_block_expression(span),
SyntaxShape::Any => {
let shapes = vec![
@ -1072,6 +1136,43 @@ impl ParserWorkingSet {
}
pub fn parse_let(&mut self, spans: &[Span]) -> (Statement, Option<ParseError>) {
if let Some(decl_id) = self.find_decl(b"let") {
let (call, call_span, err) = self.parse_internal_call(spans, decl_id);
if err.is_some() {
return (
Statement::Expression(Expression {
expr: Expr::Call(call),
span: call_span,
}),
err,
);
} else if let Expression {
expr: Expr::Var(var_id),
..
} = &call.positional[0]
{
return (
Statement::VarDecl(VarDecl {
var_id: *var_id,
expression: call.positional[2].clone(),
}),
None,
);
}
}
(
Statement::Expression(Expression {
expr: Expr::Garbage,
span: span(spans),
}),
Some(ParseError::UnknownState(
"internal error: let statement unparseable".into(),
span(spans),
)),
)
/*
let mut error = None;
if spans.len() >= 4 && self.parse_keyword(spans[0], b"let").is_none() {
let (_, err) = self.parse_variable(spans[1]);
@ -1094,6 +1195,7 @@ impl ParserWorkingSet {
Some(ParseError::Mismatch("let".into(), span)),
)
}
*/
}
pub fn parse_statement(&mut self, spans: &[Span]) -> (Statement, Option<ParseError>) {