diff options
author | Marcus Klaas de Vries <[email protected]> | 2019-01-11 10:27:07 +0000 |
---|---|---|
committer | Marcus Klaas de Vries <[email protected]> | 2019-01-14 12:56:43 +0000 |
commit | 606d66a714bb8fe07f35a6af83d04ab576b9a0e1 (patch) | |
tree | dd69d9e6484d7a67d8545ef10ec736c24d90849e | |
parent | 81bc8e4973fefd0ff31d08206c374fb58aa8b6e0 (diff) |
Start moving literal interpretation to the AST (WIP)
-rw-r--r-- | crates/ra_hir/src/expr.rs | 7 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/generated.rs | 114 | ||||
-rw-r--r-- | crates/ra_syntax/src/grammar.ron | 18 | ||||
-rw-r--r-- | crates/ra_syntax/src/syntax_kinds/generated.rs | 2 |
4 files changed, 131 insertions, 10 deletions
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 52af2af45..3f2689781 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs | |||
@@ -649,10 +649,9 @@ impl ExprCollector { | |||
649 | let exprs = e.exprs().map(|expr| self.collect_expr(expr)).collect(); | 649 | let exprs = e.exprs().map(|expr| self.collect_expr(expr)).collect(); |
650 | self.alloc_expr(Expr::Tuple { exprs }, syntax_ptr) | 650 | self.alloc_expr(Expr::Tuple { exprs }, syntax_ptr) |
651 | } | 651 | } |
652 | ast::ExprKind::Literal(e) => { | 652 | ast::ExprKind::LiteralExpr(e) => { |
653 | let child = e.syntax().children().next(); | 653 | if let Some(child) = e.literal() { |
654 | 654 | let c = child.syntax(); | |
655 | if let Some(c) = child { | ||
656 | let lit = match c.kind() { | 655 | let lit = match c.kind() { |
657 | SyntaxKind::INT_NUMBER => { | 656 | SyntaxKind::INT_NUMBER => { |
658 | let text = c.text().to_string(); | 657 | let text = c.text().to_string(); |
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 94842a514..442659aee 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs | |||
@@ -664,7 +664,7 @@ pub enum ExprKind<'a> { | |||
664 | PrefixExpr(&'a PrefixExpr), | 664 | PrefixExpr(&'a PrefixExpr), |
665 | RangeExpr(&'a RangeExpr), | 665 | RangeExpr(&'a RangeExpr), |
666 | BinExpr(&'a BinExpr), | 666 | BinExpr(&'a BinExpr), |
667 | Literal(&'a Literal), | 667 | LiteralExpr(&'a LiteralExpr), |
668 | } | 668 | } |
669 | 669 | ||
670 | impl AstNode for Expr { | 670 | impl AstNode for Expr { |
@@ -696,7 +696,7 @@ impl AstNode for Expr { | |||
696 | | PREFIX_EXPR | 696 | | PREFIX_EXPR |
697 | | RANGE_EXPR | 697 | | RANGE_EXPR |
698 | | BIN_EXPR | 698 | | BIN_EXPR |
699 | | LITERAL => Some(Expr::from_repr(syntax.into_repr())), | 699 | | LITERAL_EXPR => Some(Expr::from_repr(syntax.into_repr())), |
700 | _ => None, | 700 | _ => None, |
701 | } | 701 | } |
702 | } | 702 | } |
@@ -733,7 +733,7 @@ impl Expr { | |||
733 | PREFIX_EXPR => ExprKind::PrefixExpr(PrefixExpr::cast(&self.syntax).unwrap()), | 733 | PREFIX_EXPR => ExprKind::PrefixExpr(PrefixExpr::cast(&self.syntax).unwrap()), |
734 | RANGE_EXPR => ExprKind::RangeExpr(RangeExpr::cast(&self.syntax).unwrap()), | 734 | RANGE_EXPR => ExprKind::RangeExpr(RangeExpr::cast(&self.syntax).unwrap()), |
735 | BIN_EXPR => ExprKind::BinExpr(BinExpr::cast(&self.syntax).unwrap()), | 735 | BIN_EXPR => ExprKind::BinExpr(BinExpr::cast(&self.syntax).unwrap()), |
736 | LITERAL => ExprKind::Literal(Literal::cast(&self.syntax).unwrap()), | 736 | LITERAL_EXPR => ExprKind::LiteralExpr(LiteralExpr::cast(&self.syntax).unwrap()), |
737 | _ => unreachable!(), | 737 | _ => unreachable!(), |
738 | } | 738 | } |
739 | } | 739 | } |
@@ -849,6 +849,31 @@ impl AstNode for FieldPatList { | |||
849 | 849 | ||
850 | impl FieldPatList {} | 850 | impl FieldPatList {} |
851 | 851 | ||
852 | // FloatNumber | ||
853 | #[derive(Debug, PartialEq, Eq, Hash)] | ||
854 | #[repr(transparent)] | ||
855 | pub struct FloatNumber { | ||
856 | pub(crate) syntax: SyntaxNode, | ||
857 | } | ||
858 | unsafe impl TransparentNewType for FloatNumber { | ||
859 | type Repr = rowan::SyntaxNode<RaTypes>; | ||
860 | } | ||
861 | |||
862 | impl AstNode for FloatNumber { | ||
863 | fn cast(syntax: &SyntaxNode) -> Option<&Self> { | ||
864 | match syntax.kind() { | ||
865 | FLOAT_NUMBER => Some(FloatNumber::from_repr(syntax.into_repr())), | ||
866 | _ => None, | ||
867 | } | ||
868 | } | ||
869 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
870 | fn to_owned(&self) -> TreePtr<FloatNumber> { TreePtr::cast(self.syntax.to_owned()) } | ||
871 | } | ||
872 | |||
873 | |||
874 | impl ast::AstToken for FloatNumber {} | ||
875 | impl FloatNumber {} | ||
876 | |||
852 | // FnDef | 877 | // FnDef |
853 | #[derive(Debug, PartialEq, Eq, Hash)] | 878 | #[derive(Debug, PartialEq, Eq, Hash)] |
854 | #[repr(transparent)] | 879 | #[repr(transparent)] |
@@ -1130,6 +1155,31 @@ impl AstNode for IndexExpr { | |||
1130 | 1155 | ||
1131 | impl IndexExpr {} | 1156 | impl IndexExpr {} |
1132 | 1157 | ||
1158 | // IntNumber | ||
1159 | #[derive(Debug, PartialEq, Eq, Hash)] | ||
1160 | #[repr(transparent)] | ||
1161 | pub struct IntNumber { | ||
1162 | pub(crate) syntax: SyntaxNode, | ||
1163 | } | ||
1164 | unsafe impl TransparentNewType for IntNumber { | ||
1165 | type Repr = rowan::SyntaxNode<RaTypes>; | ||
1166 | } | ||
1167 | |||
1168 | impl AstNode for IntNumber { | ||
1169 | fn cast(syntax: &SyntaxNode) -> Option<&Self> { | ||
1170 | match syntax.kind() { | ||
1171 | INT_NUMBER => Some(IntNumber::from_repr(syntax.into_repr())), | ||
1172 | _ => None, | ||
1173 | } | ||
1174 | } | ||
1175 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1176 | fn to_owned(&self) -> TreePtr<IntNumber> { TreePtr::cast(self.syntax.to_owned()) } | ||
1177 | } | ||
1178 | |||
1179 | |||
1180 | impl ast::AstToken for IntNumber {} | ||
1181 | impl IntNumber {} | ||
1182 | |||
1133 | // ItemList | 1183 | // ItemList |
1134 | #[derive(Debug, PartialEq, Eq, Hash)] | 1184 | #[derive(Debug, PartialEq, Eq, Hash)] |
1135 | #[repr(transparent)] | 1185 | #[repr(transparent)] |
@@ -1315,10 +1365,25 @@ unsafe impl TransparentNewType for Literal { | |||
1315 | type Repr = rowan::SyntaxNode<RaTypes>; | 1365 | type Repr = rowan::SyntaxNode<RaTypes>; |
1316 | } | 1366 | } |
1317 | 1367 | ||
1368 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
1369 | pub enum LiteralKind<'a> { | ||
1370 | String(&'a String), | ||
1371 | ByteString(&'a ByteString), | ||
1372 | Char(&'a Char), | ||
1373 | Byte(&'a Byte), | ||
1374 | IntNumber(&'a IntNumber), | ||
1375 | FloatNumber(&'a FloatNumber), | ||
1376 | } | ||
1377 | |||
1318 | impl AstNode for Literal { | 1378 | impl AstNode for Literal { |
1319 | fn cast(syntax: &SyntaxNode) -> Option<&Self> { | 1379 | fn cast(syntax: &SyntaxNode) -> Option<&Self> { |
1320 | match syntax.kind() { | 1380 | match syntax.kind() { |
1321 | LITERAL => Some(Literal::from_repr(syntax.into_repr())), | 1381 | | STRING |
1382 | | BYTE_STRING | ||
1383 | | CHAR | ||
1384 | | BYTE | ||
1385 | | INT_NUMBER | ||
1386 | | FLOAT_NUMBER => Some(Literal::from_repr(syntax.into_repr())), | ||
1322 | _ => None, | 1387 | _ => None, |
1323 | } | 1388 | } |
1324 | } | 1389 | } |
@@ -1326,9 +1391,50 @@ impl AstNode for Literal { | |||
1326 | fn to_owned(&self) -> TreeArc<Literal> { TreeArc::cast(self.syntax.to_owned()) } | 1391 | fn to_owned(&self) -> TreeArc<Literal> { TreeArc::cast(self.syntax.to_owned()) } |
1327 | } | 1392 | } |
1328 | 1393 | ||
1394 | impl Literal { | ||
1395 | pub fn kind(&self) -> LiteralKind { | ||
1396 | match self.syntax.kind() { | ||
1397 | STRING => LiteralKind::String(String::cast(&self.syntax).unwrap()), | ||
1398 | BYTE_STRING => LiteralKind::ByteString(ByteString::cast(&self.syntax).unwrap()), | ||
1399 | CHAR => LiteralKind::Char(Char::cast(&self.syntax).unwrap()), | ||
1400 | BYTE => LiteralKind::Byte(Byte::cast(&self.syntax).unwrap()), | ||
1401 | INT_NUMBER => LiteralKind::IntNumber(IntNumber::cast(&self.syntax).unwrap()), | ||
1402 | FLOAT_NUMBER => LiteralKind::FloatNumber(FloatNumber::cast(&self.syntax).unwrap()), | ||
1403 | _ => unreachable!(), | ||
1404 | } | ||
1405 | } | ||
1406 | } | ||
1329 | 1407 | ||
1330 | impl Literal {} | 1408 | impl Literal {} |
1331 | 1409 | ||
1410 | // LiteralExpr | ||
1411 | #[derive(Debug, PartialEq, Eq, Hash)] | ||
1412 | #[repr(transparent)] | ||
1413 | pub struct LiteralExpr { | ||
1414 | pub(crate) syntax: SyntaxNode, | ||
1415 | } | ||
1416 | unsafe impl TransparentNewType for LiteralExpr { | ||
1417 | type Repr = rowan::SyntaxNode<RaTypes>; | ||
1418 | } | ||
1419 | |||
1420 | impl AstNode for LiteralExpr { | ||
1421 | fn cast(syntax: &SyntaxNode) -> Option<&Self> { | ||
1422 | match syntax.kind() { | ||
1423 | LITERAL_EXPR => Some(LiteralExpr::from_repr(syntax.into_repr())), | ||
1424 | _ => None, | ||
1425 | } | ||
1426 | } | ||
1427 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1428 | fn to_owned(&self) -> TreePtr<LiteralExpr> { TreePtr::cast(self.syntax.to_owned()) } | ||
1429 | } | ||
1430 | |||
1431 | |||
1432 | impl LiteralExpr { | ||
1433 | pub fn literal(&self) -> Option<&Literal> { | ||
1434 | super::child_opt(self) | ||
1435 | } | ||
1436 | } | ||
1437 | |||
1332 | // LoopExpr | 1438 | // LoopExpr |
1333 | #[derive(Debug, PartialEq, Eq, Hash)] | 1439 | #[derive(Debug, PartialEq, Eq, Hash)] |
1334 | #[repr(transparent)] | 1440 | #[repr(transparent)] |
diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index dfd88bd10..49b297696 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron | |||
@@ -215,6 +215,7 @@ Grammar( | |||
215 | "PATH", | 215 | "PATH", |
216 | "PATH_SEGMENT", | 216 | "PATH_SEGMENT", |
217 | "LITERAL", | 217 | "LITERAL", |
218 | "LITERAL_EXPR", | ||
218 | "ALIAS", | 219 | "ALIAS", |
219 | "VISIBILITY", | 220 | "VISIBILITY", |
220 | "WHERE_CLAUSE", | 221 | "WHERE_CLAUSE", |
@@ -426,11 +427,24 @@ Grammar( | |||
426 | "PrefixExpr": (options: ["Expr"]), | 427 | "PrefixExpr": (options: ["Expr"]), |
427 | "RangeExpr": (), | 428 | "RangeExpr": (), |
428 | "BinExpr": (), | 429 | "BinExpr": (), |
430 | |||
431 | "IntNumber": ( traits: ["AstToken"] ), | ||
432 | "FloatNumber": ( traits: ["AstToken"] ), | ||
429 | "String": ( traits: ["AstToken"] ), | 433 | "String": ( traits: ["AstToken"] ), |
430 | "Byte": ( traits: ["AstToken"] ), | 434 | "Byte": ( traits: ["AstToken"] ), |
431 | "ByteString": ( traits: ["AstToken"] ), | 435 | "ByteString": ( traits: ["AstToken"] ), |
432 | "Char": ( traits: ["AstToken"] ), | 436 | "Char": ( traits: ["AstToken"] ), |
433 | "Literal": (), | 437 | "Literal": ( |
438 | enum: [ | ||
439 | "String", | ||
440 | "ByteString", | ||
441 | "Char", | ||
442 | "Byte", | ||
443 | "IntNumber", | ||
444 | "FloatNumber", | ||
445 | ] | ||
446 | ), | ||
447 | "LiteralExpr": (options: ["Literal"]), | ||
434 | 448 | ||
435 | "Expr": ( | 449 | "Expr": ( |
436 | enum: [ | 450 | enum: [ |
@@ -460,7 +474,7 @@ Grammar( | |||
460 | "PrefixExpr", | 474 | "PrefixExpr", |
461 | "RangeExpr", | 475 | "RangeExpr", |
462 | "BinExpr", | 476 | "BinExpr", |
463 | "Literal", | 477 | "LiteralExpr", |
464 | ], | 478 | ], |
465 | ), | 479 | ), |
466 | 480 | ||
diff --git a/crates/ra_syntax/src/syntax_kinds/generated.rs b/crates/ra_syntax/src/syntax_kinds/generated.rs index 830fac9f4..46795d6e9 100644 --- a/crates/ra_syntax/src/syntax_kinds/generated.rs +++ b/crates/ra_syntax/src/syntax_kinds/generated.rs | |||
@@ -205,6 +205,7 @@ pub enum SyntaxKind { | |||
205 | PATH, | 205 | PATH, |
206 | PATH_SEGMENT, | 206 | PATH_SEGMENT, |
207 | LITERAL, | 207 | LITERAL, |
208 | LITERAL_EXPR, | ||
208 | ALIAS, | 209 | ALIAS, |
209 | VISIBILITY, | 210 | VISIBILITY, |
210 | WHERE_CLAUSE, | 211 | WHERE_CLAUSE, |
@@ -467,6 +468,7 @@ impl SyntaxKind { | |||
467 | PATH => &SyntaxInfo { name: "PATH" }, | 468 | PATH => &SyntaxInfo { name: "PATH" }, |
468 | PATH_SEGMENT => &SyntaxInfo { name: "PATH_SEGMENT" }, | 469 | PATH_SEGMENT => &SyntaxInfo { name: "PATH_SEGMENT" }, |
469 | LITERAL => &SyntaxInfo { name: "LITERAL" }, | 470 | LITERAL => &SyntaxInfo { name: "LITERAL" }, |
471 | LITERAL_EXPR => &SyntaxInfo { name: "LITERAL_EXPR" }, | ||
470 | ALIAS => &SyntaxInfo { name: "ALIAS" }, | 472 | ALIAS => &SyntaxInfo { name: "ALIAS" }, |
471 | VISIBILITY => &SyntaxInfo { name: "VISIBILITY" }, | 473 | VISIBILITY => &SyntaxInfo { name: "VISIBILITY" }, |
472 | WHERE_CLAUSE => &SyntaxInfo { name: "WHERE_CLAUSE" }, | 474 | WHERE_CLAUSE => &SyntaxInfo { name: "WHERE_CLAUSE" }, |