Skip to content
Draft
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
wip 3
  • Loading branch information
qingshi163 committed Jan 31, 2023
commit e80536378216ab6f06ebed24261e3761cd90af9c
25 changes: 21 additions & 4 deletions compiler/parser/src/peg_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ peg::parser! { grammar python_parser(zelf: &Parser) for Parser {
begin:position!() [Raise] {
zelf.new_located_single(begin, ast::StmtKind::Raise { exc: None, cause: None })
}

rule global_stmt() -> ast::Stmt = begin:position!() [Global] a:([Name { name }] { name.clone() }) ++ [Comma] end:position!() {
zelf.new_located(begin, end, ast::StmtKind::Global { names: a })
}

rule nonlocal_stmt() -> ast::Stmt = begin:position!() [Nonlocal] a:([Name { name }] { name.clone() }) ++ [Comma] end:position!() {
zelf.new_located(begin, end, ast::StmtKind::Nonlocal { names: a })
}
Expand Down Expand Up @@ -186,12 +186,12 @@ peg::parser! { grammar python_parser(zelf: &Parser) for Parser {
begin:position!() [From] a:[Dot | Ellipsis]+ [Import] b:import_from_targets() end:position!() {
zelf.new_located(begin, end, ast::StmtKind::ImportFrom { module: None, names: b, level: count_dots(a) })
}

rule import_from_targets() -> Vec<ast::Alias> =
[Lpar] a:import_from_as_names() [Comma]? [Rpar] { a } /
a:import_from_as_names() ![Comma] { a } /
begin:position!() [Star] { vec![zelf.new_located_single(begin, ast::AliasData { name: "*".to_owned(), asname: None })] }

rule import_from_as_names() -> Vec<ast::Alias> = import_from_as_name() ++ [Comma]

rule import_from_as_name() -> ast::Alias = begin:position!() [Name { name }] b:([As] [Name { name }] { name })? end:position!() {
Expand All @@ -211,6 +211,23 @@ peg::parser! { grammar python_parser(zelf: &Parser) for Parser {
} /
[Name { name }] { name.clone() }

rule block() -> Vec<ast::Stmt> =
[Newline] [Indent] a:statements() [Dedent] { a } /
simple_stmts()

rule decorators() -> Vec<ast::Expr> = ([At] f:named_expression() [Newline] { f })+

// rule class_def() -> ast::Stmt =
// a:decorators() b:class_def_raw() {

// } /
// class_def_raw()

// rule class_def_raw() -> ast::StmtKind =
// begin:position!() [Class] [Name { name }] b:([Lpar] z:arguments()? [Rpar]) [Colon] c:block() end:position!() {
// zelf.new_located(begin, end, ast::StmtKind::ClassDef { name: name.clone(), bases: b, keywords: b, body: c, decorator_list: vec![] })
// }

rule expressions() -> Vec<ast::Expr> = a:expression() ++ [Comma] [Comma]? { a }

// rule expression() -> ast:Expr =
Expand Down