Skip to content

Commit

Permalink
final (rebase this somehow or something idk)
Browse files Browse the repository at this point in the history
  • Loading branch information
jprochazk committed Feb 26, 2023
1 parent 8d3d9d4 commit 5222052
Show file tree
Hide file tree
Showing 253 changed files with 1,173 additions and 567 deletions.
Binary file not shown.
48 changes: 30 additions & 18 deletions crates/syntax/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,23 @@ pub enum StmtKind<'src> {
}

#[cfg_attr(test, derive(Debug))]
pub struct Import<'src> {
pub symbols: Vec<ImportSymbol<'src>>,
pub enum Import<'src> {
Module {
path: Vec<Ident<'src>>,
alias: Option<Ident<'src>>,
},
Symbols {
path: Vec<Ident<'src>>,
symbols: Vec<ImportSymbol<'src>>,
},
}

#[cfg_attr(test, derive(Debug))]
pub struct ImportSymbol<'src> {
pub path: Vec<Ident<'src>>,
pub name: Ident<'src>,
pub alias: Option<Ident<'src>>,
}

impl<'src> ImportSymbol<'src> {
pub fn normal(path: Vec<Ident<'src>>) -> Self {
ImportSymbol { path, alias: None }
}

pub fn alias(path: Vec<Ident<'src>>, alias: Ident<'src>) -> Self {
ImportSymbol {
path,
alias: Some(alias),
}
}
}

#[cfg_attr(test, derive(Debug))]
pub struct Func<'src> {
pub name: Ident<'src>,
Expand Down Expand Up @@ -372,8 +366,26 @@ pub enum Ctrl<'src> {
Break,
}

pub fn import_stmt<'src>(s: impl Into<Span>, symbols: Vec<ImportSymbol<'src>>) -> Stmt<'src> {
Stmt::new(s, StmtKind::Import(Box::new(Import { symbols })))
pub fn import_module_stmt<'src>(
s: impl Into<Span>,
path: Vec<Ident<'src>>,
alias: Option<Ident<'src>>,
) -> Stmt<'src> {
Stmt::new(
s,
StmtKind::Import(Box::new(Import::Module { path, alias })),
)
}

pub fn import_symbols_stmt<'src>(
s: impl Into<Span>,
path: Vec<Ident<'src>>,
symbols: Vec<ImportSymbol<'src>>,
) -> Stmt<'src> {
Stmt::new(
s,
StmtKind::Import(Box::new(Import::Symbols { path, symbols })),
)
}

pub fn if_stmt<'src>(
Expand Down
3 changes: 3 additions & 0 deletions crates/syntax/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ pub enum TokenKind {
// Keywords
#[token("import")]
Kw_Import,
#[token("from")]
Kw_From,
#[token("as")]
Kw_As,
#[token("pub")]
Expand Down Expand Up @@ -278,6 +280,7 @@ impl TokenKind {
pub fn name(&self) -> &'static str {
match self {
TokenKind::Kw_Import => "import",
TokenKind::Kw_From => "from",
TokenKind::Kw_As => "as",
TokenKind::Kw_Pub => "pub",
TokenKind::Kw_Fn => "fn",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,16 +502,16 @@ expression: tokens
(>_ Brk_ParenR @1840..1841),
(>0 Kw_Import @1867..1873),
(>_ Lit_Ident `json` @1874..1878),
(>0 Lit_Ident `v` @2008..2009),
(>_ Op_Equal @2010..2011),
(>_ Lit_Ident `json` @2012..2016),
(>_ Op_Dot @2016..2017),
(>_ Lit_Ident `parse` @2017..2022),
(>_ Brk_ParenL @2022..2023),
(>_ Lit_String @2023..2043),
(>_ Brk_ParenR @2043..2044),
(>0 Kw_Print @2045..2050),
(>_ Brk_ParenL @2050..2051),
(>_ Lit_Ident `v` @2051..2052),
(>_ Brk_ParenR @2052..2053),
(>0 Lit_Ident `v` @1880..1881),
(>_ Op_Equal @1882..1883),
(>_ Lit_Ident `json` @1884..1888),
(>_ Op_Dot @1888..1889),
(>_ Lit_Ident `parse` @1889..1894),
(>_ Brk_ParenL @1894..1895),
(>_ Lit_String @1895..1915),
(>_ Brk_ParenR @1915..1916),
(>0 Kw_Print @1917..1922),
(>_ Brk_ParenL @1922..1923),
(>_ Lit_Ident `v` @1923..1924),
(>_ Brk_ParenR @1924..1925),
]
6 changes: 0 additions & 6 deletions crates/syntax/src/lexer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,6 @@ panic("asdf")
# modules
# json_test.t
import json
# other ways to import:
# import json.parse
# import json.{parse}
# import {json}
# import {json.parse}
# import {json.{parse}}
v = json.parse("{\"a\":0, \"b\":1}")
print(v) # { a: 0, b: 1 }
Expand Down
2 changes: 1 addition & 1 deletion crates/syntax/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ impl<'src> Parser<'src> {

match self.current().kind {
// break on keywords that begin statements
Kw_Import | Kw_Fn | Kw_Class | Kw_For | Kw_While | Kw_Loop | Kw_If => break,
Kw_Import | Kw_From | Kw_Fn | Kw_Class | Kw_For | Kw_While | Kw_Loop | Kw_If => break,
// handle any errors
Tok_Error => self.errors.push(Error::new(
format!("invalid token `{}`", self.lex.lexeme(self.current())),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
---
source: crates/syntax/src/parser/tests.rs
expression: module
---
Module {
body: [
Import(
Module {
path: [
"module",
],
alias: Some(
"temp",
),
},
),
Import(
Symbols {
path: [
"module",
],
symbols: [
ImportSymbol {
name: "z",
alias: Some(
"temp",
),
},
],
},
),
Import(
Symbols {
path: [
"module",
],
symbols: [
ImportSymbol {
name: "x",
alias: Some(
"temp",
),
},
ImportSymbol {
name: "y",
alias: Some(
"temp",
),
},
ImportSymbol {
name: "z",
alias: Some(
"temp",
),
},
],
},
),
Import(
Module {
path: [
"module",
"nested",
],
alias: Some(
"temp",
),
},
),
Import(
Symbols {
path: [
"module",
"nested",
],
symbols: [
ImportSymbol {
name: "z",
alias: Some(
"temp",
),
},
],
},
),
Import(
Symbols {
path: [
"module",
"nested",
],
symbols: [
ImportSymbol {
name: "x",
alias: Some(
"temp",
),
},
ImportSymbol {
name: "y",
alias: Some(
"temp",
),
},
ImportSymbol {
name: "z",
alias: Some(
"temp",
),
},
],
},
),
],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: crates/syntax/src/parser/tests.rs
expression: errors
---
error: invalid indentation
> code:2
|
| from m import b
|

error: invalid indentation
> code:2
|
| from m import b
|


Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: crates/syntax/src/parser/tests.rs
expression: errors
---
error: invalid indentation
> code:2
|
| m
|


Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: crates/syntax/src/parser/tests.rs
expression: errors
---
error: invalid indentation
> code:2
|
| a
|


Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: crates/syntax/src/parser/tests.rs
expression: errors
---
error: invalid indentation
> code:2
|
| b
|


Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: crates/syntax/src/parser/tests.rs
expression: errors
---
error: invalid indentation
> code:2
|
| .b
|


Loading

0 comments on commit 5222052

Please sign in to comment.