mirror of
https://github.com/AleoHQ/leo.git
synced 2024-11-11 04:49:15 +03:00
1st draft for string parsing and ast type
- currently uses back quotes "`" for strings, change later - ast -> asg unimplemented, strings need to be processed on canonicalization stage
This commit is contained in:
parent
1377fc5d75
commit
32bd282030
@ -220,6 +220,9 @@ impl<'a> FromAst<'a, leo_ast::ValueExpression> for Constant<'a> {
|
||||
value: ConstValue::Int(ConstInt::parse(int_type, value, span)?),
|
||||
}
|
||||
}
|
||||
String(_str_type, _value) => {
|
||||
unimplemented!("strings do not exist on ASG level")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -33,6 +33,7 @@ pub enum ValueExpression {
|
||||
#[serde(with = "crate::common::tendril_json")] StrTendril,
|
||||
Span,
|
||||
),
|
||||
String(Vec<char>, Span),
|
||||
}
|
||||
|
||||
impl fmt::Display for ValueExpression {
|
||||
@ -46,6 +47,12 @@ impl fmt::Display for ValueExpression {
|
||||
Implicit(implicit, _) => write!(f, "{}", implicit),
|
||||
Integer(value, type_, _) => write!(f, "{}{}", value, type_),
|
||||
Group(group) => write!(f, "{}", group),
|
||||
String(char_vec, _) => {
|
||||
for character in char_vec {
|
||||
write!(f, "{}", character)?
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -59,7 +66,8 @@ impl Node for ValueExpression {
|
||||
| Char(_, span)
|
||||
| Field(_, span)
|
||||
| Implicit(_, span)
|
||||
| Integer(_, _, span) => span,
|
||||
| Integer(_, _, span) // => span,
|
||||
| String(_, span) => span,
|
||||
Group(group) => match &**group {
|
||||
GroupValue::Single(_, span) | GroupValue::Tuple(GroupTuple { span, .. }) => span,
|
||||
},
|
||||
@ -74,7 +82,8 @@ impl Node for ValueExpression {
|
||||
| Char(_, span)
|
||||
| Field(_, span)
|
||||
| Implicit(_, span)
|
||||
| Integer(_, _, span) => *span = new_span,
|
||||
| Integer(_, _, span) // => *span = new_span,
|
||||
| String(_, span) => *span = new_span,
|
||||
Group(group) => match &mut **group {
|
||||
GroupValue::Single(_, span) | GroupValue::Tuple(GroupTuple { span, .. }) => *span = new_span,
|
||||
},
|
||||
|
@ -690,6 +690,7 @@ impl ParserContext {
|
||||
Token::False => Expression::Value(ValueExpression::Boolean("false".into(), span)),
|
||||
Token::AddressLit(value) => Expression::Value(ValueExpression::Address(value, span)),
|
||||
Token::CharLit(value) => Expression::Value(ValueExpression::Char(value, span)),
|
||||
Token::QuotedString(value) => Expression::Value(ValueExpression::String(value, span)),
|
||||
Token::LeftParen => self.parse_tuple_expression(&span)?,
|
||||
Token::LeftSquare => self.parse_array_expression(&span)?,
|
||||
Token::Ident(name) => {
|
||||
|
@ -235,6 +235,16 @@ impl Token {
|
||||
let input = input_tendril[..].as_bytes();
|
||||
match input[0] {
|
||||
x if x.is_ascii_whitespace() => return (1, None),
|
||||
b'`' => {
|
||||
let mut collect: Vec<char> = Vec::new();
|
||||
for (i, char_bytes) in input.iter().enumerate().skip(1) {
|
||||
if *char_bytes == b'`' {
|
||||
return (i + 1, Some(Token::QuotedString(collect)));
|
||||
}
|
||||
|
||||
collect.push(std::char::from_u32(*char_bytes as u32).unwrap());
|
||||
}
|
||||
}
|
||||
b'"' => {
|
||||
let mut i = 1;
|
||||
let mut in_escape = false;
|
||||
|
@ -259,6 +259,6 @@ mod tests {
|
||||
let original = &raw[*start + token.span.col_start - 1..*stop + token.span.col_stop - 1];
|
||||
assert_eq!(original, &token_raw);
|
||||
}
|
||||
println!("{}", serde_json::to_string_pretty(&tokens).unwrap());
|
||||
// println!("{}", serde_json::to_string_pretty(&tokens).unwrap());
|
||||
}
|
||||
}
|
||||
|
@ -42,6 +42,7 @@ pub enum Token {
|
||||
CommentLine(#[serde(with = "leo_ast::common::tendril_json")] StrTendril),
|
||||
CommentBlock(#[serde(with = "leo_ast::common::tendril_json")] StrTendril),
|
||||
FormatString(Vec<FormatStringPart>),
|
||||
QuotedString(Vec<char>),
|
||||
Ident(#[serde(with = "leo_ast::common::tendril_json")] StrTendril),
|
||||
Int(#[serde(with = "leo_ast::common::tendril_json")] StrTendril),
|
||||
True,
|
||||
@ -215,6 +216,13 @@ impl fmt::Display for Token {
|
||||
}
|
||||
write!(f, "\"")
|
||||
}
|
||||
QuotedString(content) => {
|
||||
write!(f, "\"")?;
|
||||
for character in content {
|
||||
write!(f, "{}", character)?;
|
||||
}
|
||||
write!(f, "\"")
|
||||
}
|
||||
Ident(s) => write!(f, "{}", s),
|
||||
Int(s) => write!(f, "{}", s),
|
||||
True => write!(f, "true"),
|
||||
|
63
tests/expectations/parser/parser/expression/string.leo.out
Normal file
63
tests/expectations/parser/parser/expression/string.leo.out
Normal file
@ -0,0 +1,63 @@
|
||||
---
|
||||
namespace: ParseExpression
|
||||
expectation: Pass
|
||||
outputs:
|
||||
- Value:
|
||||
String:
|
||||
- - s
|
||||
- t
|
||||
- r
|
||||
- i
|
||||
- n
|
||||
- g
|
||||
- line_start: 1
|
||||
line_stop: 1
|
||||
col_start: 1
|
||||
col_stop: 9
|
||||
path: test
|
||||
content: "`string`"
|
||||
- Value:
|
||||
String:
|
||||
- - a
|
||||
- n
|
||||
- o
|
||||
- t
|
||||
- h
|
||||
- e
|
||||
- r
|
||||
- " "
|
||||
- "{"
|
||||
- " "
|
||||
- "}"
|
||||
- " "
|
||||
- s
|
||||
- t
|
||||
- r
|
||||
- i
|
||||
- n
|
||||
- g
|
||||
- line_start: 1
|
||||
line_stop: 1
|
||||
col_start: 1
|
||||
col_stop: 21
|
||||
path: test
|
||||
content: "`another { } string`"
|
||||
- Value:
|
||||
String:
|
||||
- - "{"
|
||||
- " "
|
||||
- "\\"
|
||||
- " "
|
||||
- "]"
|
||||
- " "
|
||||
- "["
|
||||
- " "
|
||||
- ;
|
||||
- " "
|
||||
- a
|
||||
- line_start: 1
|
||||
line_stop: 1
|
||||
col_start: 1
|
||||
col_stop: 14
|
||||
path: test
|
||||
content: "`{ \\ ] [ ; a`"
|
10
tests/parser/expression/string.leo
Normal file
10
tests/parser/expression/string.leo
Normal file
@ -0,0 +1,10 @@
|
||||
/*
|
||||
namespace: ParseExpression
|
||||
expectation: Pass
|
||||
*/
|
||||
|
||||
`string`
|
||||
|
||||
`another { } string`
|
||||
|
||||
`{ \ ] [ ; a`
|
Loading…
Reference in New Issue
Block a user