Skip to content

Feature Request: Allow default tokens to be returned, for optional tokens #32

@oxcrow

Description

@oxcrow

Hi,

Consider these statements ...

let x = 1;
let const x = 1;

fn main() {}
fn main() -> void {}

We often have such instances where the default token is assumed, if it is not given by the user.

Thus, it would be easier for us, if the default tokens were returned if optional tokens don't exist in our rules.

This would be helpful, as currently to represent the return type of the functions, I would need to use an Option<>, and modify the AST nodes after parsing, to set the function's return type as void.

// in pest file
function = { "fn" ~ id ~ "(" ~ (args)? ~ ")" ~ ("->" ~ type)? ~ "{" ~ function_item* ~ "}" }

// in rust file
#[derive(Debug, Clone, FromPest)]
#[pest_ast(rule(Rule::function))]
pub struct Function {
    pub id: Id,
    pub args: Args,
    pub r#type: Option<Type>, // <<< Issue ... created by optional rule ("->" ~ type)? above
    pub items: Vec<FunctionItem>,
}

This is rather tedious work, and complicates our compiler's AST representation.

In OCaml's Menhir parser generator, if a rule doesn't match anything, we are allowed to return a default node.

Such a feature would also be extremely helpful for pest.

type:
  | (* empty *) { Ast.Type_Void } (* <<< If nothing exists for return type token, then return a Void type  *)
  | ARROW INT { Ast.Type_Int }
  | ARROW id = ID; { Ast.Type_Derived id }
  ;

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions