From f797cb5855d149c679525f55b1558a5dcc27d996 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Sat, 16 Jul 2005 14:07:35 +0000 Subject: [PATCH] * Revive and update the SDF grammar for Nix expressions. --- src/libexpr/Makefile.am | 8 +++ src/libexpr/nix.sdf | 141 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 src/libexpr/nix.sdf diff --git a/src/libexpr/Makefile.am b/src/libexpr/Makefile.am index 0186f16d9..9af7404bc 100644 --- a/src/libexpr/Makefile.am +++ b/src/libexpr/Makefile.am @@ -33,3 +33,11 @@ nixexpr.cc nixexpr.hh: nixexpr-ast.hh CLEANFILES = + + +# SDF stuff (not built by default). +nix.tbl: nix.sdf + sdf2table -m Nix -s -i nix.sdf -o nix.tbl + +test.ast: test.nix nix.tbl + sglri -p nix.tbl -i test.nix -o test.ast diff --git a/src/libexpr/nix.sdf b/src/libexpr/nix.sdf new file mode 100644 index 000000000..a123829e3 --- /dev/null +++ b/src/libexpr/nix.sdf @@ -0,0 +1,141 @@ +definition + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% Top level syntax. + +module Nix +imports Nix-Exprs Nix-Layout + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% Expressions. + +module Nix-Exprs +imports Nix-Lexicals +exports + sorts Expr Formal Bind Binds ExprList + context-free start-symbols Expr + context-free syntax + + Id -> Expr {cons("Var")} + Int -> Expr {cons("Int")} + Str -> Expr {cons("Str")} + Uri -> Expr {cons("Uri")} + Path -> Expr {cons("Path")} + + "(" Expr ")" -> Expr {bracket} + + Expr Expr -> Expr {cons("Call"), left} + + Id ":" Expr -> Expr {cons("Function1")} + "{" {Formal ","}* "}" ":" Expr -> Expr {cons("Function")} + Id -> Formal {cons("NoDefFormal")} + Id "?" Expr -> Formal {cons("DefFormal")} + + "assert" Expr ";" Expr -> Expr {cons("Assert")} + + "with" Expr ";" Expr -> Expr {cons("With")} + + "rec" "{" Binds "}" -> Expr {cons("Rec")} + "let" "{" Binds "}" -> Expr {cons("LetRec")} + "{" Binds "}" -> Expr {cons("Attrs")} + + Bind* -> Binds + Id "=" Expr ";" -> Bind {cons("Bind")} + "inherit" ("(" Expr ")")? Id* ";" -> Bind {cons("Inherit")} + + "[" ExprList "]" -> Expr {cons("List")} + "" -> ExprList {cons("ExprNil")} + Expr ExprList -> ExprList {cons("ExprCons")} + + Expr "." Id -> Expr {cons("Select")} + + "if" Expr "then" Expr "else" Expr -> Expr {cons("If")} + + Expr "==" Expr -> Expr {cons("OpEq"), non-assoc} + Expr "!=" Expr -> Expr {cons("OpNEq"), non-assoc} + + "!" Expr -> Expr {cons("OpNot")} + Expr "&&" Expr -> Expr {cons("OpAnd"), right} + Expr "||" Expr -> Expr {cons("OpOr"), right} + Expr "->" Expr -> Expr {cons("OpImpl"), right} + + Expr "//" Expr -> Expr {cons("OpUpdate"), right} + Expr "~" Expr -> Expr {cons("SubPath"), non-assoc} + Expr "?" Id -> Expr {cons("OpHasAttr")} + Expr "+" Expr -> Expr {cons("OpPlus"), left} + + context-free priorities + + Expr "." Id -> Expr + > Expr ExprList -> ExprList + > Expr Expr -> Expr + > Expr "~" Expr -> Expr + > Expr "?" Id -> Expr + > Expr "+" Expr -> Expr + > "!" Expr -> Expr + > Expr "//" Expr -> Expr + > Expr "==" Expr -> Expr + > Expr "!=" Expr -> Expr + > Expr "&&" Expr -> Expr + > Expr "||" Expr -> Expr + > Expr "->" Expr -> Expr + > "assert" Expr ";" Expr -> Expr + > "with" Expr ";" Expr -> Expr + > Id ":" Expr -> Expr + > "{" {Formal ","}* "}" ":" Expr -> Expr + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% Lexical syntax. + +module Nix-Lexicals +exports + sorts Id Int Str Path PathComp Uri + lexical syntax + [a-zA-Z\_][a-zA-Z0-9\_\']* -> Id + "rec" -> Id {reject} + "let" -> Id {reject} + "if" -> Id {reject} + "then" -> Id {reject} + "else" -> Id {reject} + "assert" -> Id {reject} + "inherit" -> Id {reject} + + [0-9]+ -> Int + + "\"" ~[\n\"]* "\"" -> Str + + "." ("/" PathComp)+ -> Path + ".." ("/" PathComp)+ -> Path + ("/" PathComp)+ -> Path + [a-zA-Z0-9\.\_\-\+]+ -> PathComp + + [a-zA-Z] [a-zA-Z0-9\+\-\.]* ":" [a-zA-Z0-9\%\/\?\:\@\&\=\+\$\,\-\_\.\!\~\*\']* -> Uri + + lexical restrictions + Id -/- [a-zA-Z0-9\_\'] + Int -/- [0-9] + Path -/- [a-zA-Z0-9\.\_\-\+\/] + Uri -/- [a-zA-Z0-9\%\/\?\:\@\&\=\+\$\,\-\_\.\!\~\*\'] + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% Layout. + +module Nix-Layout +exports + sorts HashComment Asterisk Comment + lexical syntax + [\ \t\n] -> LAYOUT + HashComment -> LAYOUT + Comment -> LAYOUT + "#" ~[\n]* -> HashComment + "/*" ( ~[\*] | Asterisk )* "*/" -> Comment + [\*] -> Asterisk + lexical restrictions + Asterisk -/- [\/] + HashComment -/- ~[\n] + context-free restrictions + LAYOUT? -/- [\ \t\n]