gdritter repos datalog / master src / grammar.lalrpop
master

Tree @master (Download .tar.gz)

grammar.lalrpop @masterraw · history · blame

use std::str::FromStr;
use ast::*;

grammar;

pub TopLevel: TopLevel = {
  "table" <c:Clause<Type>> "." => TopLevel::TableDecl(c),
  "insert" <c:Clause<Expr>> "." => TopLevel::RowDecl(c),
};

pub RowDecl: Clause<Expr> = Clause<Expr>;

Clause<T>: Clause<T> = {
  <i:Ident> <d:Data<T>> => Clause { name: i, data: d }
};

Type: Type = {
  "atom" => Type::AtomType,
  "int"  => Type::IntType,
};

Expr: Expr = {
  <Ident> => Expr::Atom(<>),
  <Var> => Expr::Var(<>),
  <Num> => Expr::Num(<>),
};

Data<T>: ClauseData<T> = {
  "(" <ls: Comma<T>> ")" => ClauseData::TupleClause(ls),
  "{" <ls: Comma<Field<T>>> "}" => ClauseData::RecordClause(ls),
};

Field<T>: Field<T> = {
  <i:Ident> ":" <t: T> => Field { name: i, value: t },
};

Comma<T>: Vec<T> = {
  <v:(<T> ",")*> <e: T?> => match e {
    None => v,
    Some(e) => {
      let mut v = v; v.push(e); v
    }
  }
};

Ident: String = <s:r"[a-z_][A-Za-z0-9_]*"> => s.to_string();
Var: String = <s:r"[A-Z][A-Za-z0-9_]*"> => s.to_string();

Num: i64 = <s:r"[0-9]*"> => i64::from_str(s).unwrap();