diff options
author | Eelco Dolstra <e.dolstra@tudelft.nl> | 2006-09-04T21·36+0000 |
---|---|---|
committer | Eelco Dolstra <e.dolstra@tudelft.nl> | 2006-09-04T21·36+0000 |
commit | e3ce954582f56b9d853ea379c783cf6cd5571c83 (patch) | |
tree | b4882e00f5ecccbabcfb5c4f37427d01149b585f /src | |
parent | 75068e7d753cf6cbe45a4bf294000dca9bd41d8b (diff) |
* Compile the lexer as C++ code. Remove all the redundant C/C++
marshalling code.
Diffstat (limited to 'src')
-rw-r--r-- | src/libexpr/Makefile.am | 10 | ||||
-rw-r--r-- | src/libexpr/lexer.l | 44 | ||||
-rw-r--r-- | src/libexpr/parser.cc | 244 | ||||
-rw-r--r-- | src/libexpr/parser.y | 212 |
4 files changed, 234 insertions, 276 deletions
diff --git a/src/libexpr/Makefile.am b/src/libexpr/Makefile.am index 3696e99d54f6..e80adaf02283 100644 --- a/src/libexpr/Makefile.am +++ b/src/libexpr/Makefile.am @@ -1,14 +1,14 @@ pkglib_LTLIBRARIES = libexpr.la -libexpr_la_SOURCES = nixexpr.cc nixexpr.hh parser.cc parser.hh \ +libexpr_la_SOURCES = nixexpr.cc nixexpr.hh \ eval.cc eval.hh primops.cc \ - lexer-tab.c lexer-tab.h parser-tab.cc parser-tab.hh \ + lexer-tab.cc lexer-tab.hh parser-tab.cc parser-tab.hh \ get-drvs.cc get-drvs.hh \ attr-path.cc attr-path.hh \ expr-to-xml.cc expr-to-xml.hh BUILT_SOURCES = nixexpr-ast.cc nixexpr-ast.hh \ - parser-tab.hh lexer-tab.h parser-tab.cc lexer-tab.c + parser-tab.hh lexer-tab.hh parser-tab.cc lexer-tab.cc EXTRA_DIST = lexer.l parser.y nixexpr-ast.def nixexpr-ast.cc @@ -24,8 +24,8 @@ AM_CFLAGS = \ parser-tab.cc parser-tab.hh: parser.y $(bison) -v -o parser-tab.cc $(srcdir)/parser.y -d -lexer-tab.c lexer-tab.h: lexer.l - $(flex) --outfile lexer-tab.c --header-file=lexer-tab.h $(srcdir)/lexer.l +lexer-tab.cc lexer-tab.hh: lexer.l + $(flex) --outfile lexer-tab.cc --header-file=lexer-tab.hh $(srcdir)/lexer.l # ATerm helper function generation. diff --git a/src/libexpr/lexer.l b/src/libexpr/lexer.l index bbf872ff6ec8..625e044b682f 100644 --- a/src/libexpr/lexer.l +++ b/src/libexpr/lexer.l @@ -7,16 +7,23 @@ %{ -#include <string.h> -#include <aterm2.h> +#include "aterm.hh" +#include "nixexpr.hh" +#include "nixexpr-ast.hh" #include "parser-tab.hh" +using namespace nix; + +namespace nix { + + static void initLoc(YYLTYPE * loc) { loc->first_line = 1; loc->first_column = 1; } + static void adjustLoc(YYLTYPE * loc, const char * s, size_t len) { while (len--) { @@ -35,12 +42,32 @@ static void adjustLoc(YYLTYPE * loc, const char * s, size_t len) } } -ATerm toATerm(const char * s) + +static Expr unescapeStr(const char * s) { - return (ATerm) ATmakeAppl0(ATmakeAFun((char *) s, 0, ATtrue)); + string t; + char c; + while ((c = *s++)) { + if (c == '\\') { + assert(*s); + c = *s++; + if (c == 'n') t += '\n'; + else if (c == 'r') t += '\r'; + else if (c == 't') t += '\t'; + else t += c; + } + else if (c == '\r') { + /* Normalise CR and CR/LF into LF. */ + t += '\n'; + if (*s == '\n') s++; /* cr/lf */ + } + else t += c; + } + return makeStr(toATerm(t)); } -ATerm unescapeStr(const char * s); + +} #define YY_USER_INIT initLoc(yylloc) #define YY_USER_ACTION adjustLoc(yylloc, yytext, yyleng); @@ -106,12 +133,17 @@ inherit { return INHERIT; } %% + +namespace nix { + /* Horrible, disgusting hack: allow the parser to set the scanner start condition back to STRING. Necessary in interpolations like "foo${expr}bar"; after the close brace we have to go back to the STRING state. */ void backToString(yyscan_t scanner) { - struct yyguts_t * yyg = (struct yyguts_t*) scanner; + struct yyguts_t * yyg = (struct yyguts_t *) scanner; BEGIN(STRING); } + +} diff --git a/src/libexpr/parser.cc b/src/libexpr/parser.cc deleted file mode 100644 index 20a6c9be2efd..000000000000 --- a/src/libexpr/parser.cc +++ /dev/null @@ -1,244 +0,0 @@ -#include "parser.hh" -#include "aterm.hh" -#include "util.hh" -#include "nixexpr-ast.hh" - -#include <sstream> - -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <unistd.h> - - -extern "C" { - -#include "parser-tab.hh" -#include "lexer-tab.h" - -} - -namespace nix { - - -struct ParseData -{ - Expr result; - Path basePath; - Path path; - string error; -}; - -} - - -int yyparse(yyscan_t scanner, nix::ParseData * data); - - -namespace nix { - - -void setParseResult(ParseData * data, ATerm t) -{ - data->result = t; -} - -ATerm absParsedPath(ParseData * data, ATerm t) -{ - return toATerm(absPath(aterm2String(t), data->basePath)); -} - -void parseError(ParseData * data, char * error, int line, int column) -{ - data->error = (format("%1%, at `%2%':%3%:%4%") - % error % data->path % line % column).str(); -} - -ATerm fixAttrs(int recursive, ATermList as) -{ - ATermList bs = ATempty, cs = ATempty; - ATermList * is = recursive ? &cs : &bs; - for (ATermIterator i(as); i; ++i) { - ATermList names; - Expr src; - ATerm pos; - if (matchInherit(*i, src, names, pos)) { - bool fromScope = matchScope(src); - for (ATermIterator j(names); j; ++j) { - Expr rhs = fromScope ? makeVar(*j) : makeSelect(src, *j); - *is = ATinsert(*is, makeBind(*j, rhs, pos)); - } - } else bs = ATinsert(bs, *i); - } - if (recursive) - return makeRec(bs, cs); - else - return makeAttrs(bs); -} - -const char * getPath(ParseData * data) -{ - return data->path.c_str(); -} - -extern "C" { -Expr unescapeStr(const char * s) -{ - string t; - char c; - while ((c = *s++)) { - if (c == '\\') { - assert(*s); - c = *s++; - if (c == 'n') t += '\n'; - else if (c == 'r') t += '\r'; - else if (c == 't') t += '\t'; - else t += c; - } - else if (c == '\r') { - /* Normalise CR and CR/LF into LF. */ - t += '\n'; - if (*s == '\n') s++; /* cr/lf */ - } - else t += c; - } - return makeStr(toATerm(t)); -} -} - - -static void checkAttrs(ATermMap & names, ATermList bnds) -{ - for (ATermIterator i(bnds); i; ++i) { - ATerm name; - Expr e; - ATerm pos; - if (!matchBind(*i, name, e, pos)) abort(); /* can't happen */ - if (names.get(name)) - throw EvalError(format("duplicate attribute `%1%' at %2%") - % aterm2String(name) % showPos(pos)); - names.set(name, name); - } -} - - -static void checkAttrSets(ATerm e) -{ - ATermList formals; - ATerm body, pos; - if (matchFunction(e, formals, body, pos)) { - ATermMap names(ATgetLength(formals)); - for (ATermIterator i(formals); i; ++i) { - ATerm name; - ATerm d1, d2; - if (!matchFormal(*i, name, d1, d2)) abort(); - if (names.get(name)) - throw EvalError(format("duplicate formal function argument `%1%' at %2%") - % aterm2String(name) % showPos(pos)); - names.set(name, name); - } - } - - ATermList bnds; - if (matchAttrs(e, bnds)) { - ATermMap names(ATgetLength(bnds)); - checkAttrs(names, bnds); - } - - ATermList rbnds, nrbnds; - if (matchRec(e, rbnds, nrbnds)) { - ATermMap names(ATgetLength(rbnds) + ATgetLength(nrbnds)); - checkAttrs(names, rbnds); - checkAttrs(names, nrbnds); - } - - if (ATgetType(e) == AT_APPL) { - int arity = ATgetArity(ATgetAFun(e)); - for (int i = 0; i < arity; ++i) - checkAttrSets(ATgetArgument(e, i)); - } - - else if (ATgetType(e) == AT_LIST) - for (ATermIterator i((ATermList) e); i; ++i) - checkAttrSets(*i); -} - - -static Expr parse(EvalState & state, - const char * text, const Path & path, - const Path & basePath) -{ - yyscan_t scanner; - ParseData data; - data.basePath = basePath; - data.path = path; - - yylex_init(&scanner); - yy_scan_string(text, scanner); - int res = yyparse(scanner, &data); - yylex_destroy(scanner); - - if (res) throw EvalError(data.error); - - try { - checkVarDefs(state.primOps, data.result); - } catch (Error & e) { - throw EvalError(format("%1%, in `%2%'") % e.msg() % path); - } - - checkAttrSets(data.result); - - return data.result; -} - - -Expr parseExprFromFile(EvalState & state, Path path) -{ - SwitchToOriginalUser sw; - - assert(path[0] == '/'); - -#if 0 - /* Perhaps this is already an imploded parse tree? */ - Expr e = ATreadFromNamedFile(path.c_str()); - if (e) return e; -#endif - - /* If `path' is a symlink, follow it. This is so that relative - path references work. */ - struct stat st; - if (lstat(path.c_str(), &st)) - throw SysError(format("getting status of `%1%'") % path); - if (S_ISLNK(st.st_mode)) path = absPath(readLink(path), dirOf(path)); - - /* If `path' refers to a directory, append `/default.nix'. */ - if (stat(path.c_str(), &st)) - throw SysError(format("getting status of `%1%'") % path); - if (S_ISDIR(st.st_mode)) - path = canonPath(path + "/default.nix"); - - /* Read the input file. We can't use SGparseFile() because it's - broken, so we read the input ourselves and call - SGparseString(). */ - AutoCloseFD fd = open(path.c_str(), O_RDONLY); - if (fd == -1) throw SysError(format("opening `%1%'") % path); - - if (fstat(fd, &st) == -1) - throw SysError(format("statting `%1%'") % path); - - char text[st.st_size + 1]; - readFull(fd, (unsigned char *) text, st.st_size); - text[st.st_size] = 0; - - return parse(state, text, path, dirOf(path)); -} - - -Expr parseExprFromString(EvalState & state, - const string & s, const Path & basePath) -{ - return parse(state, s.c_str(), "(string)", basePath); -} - - -} diff --git a/src/libexpr/parser.y b/src/libexpr/parser.y index 7714a5911165..1f141c7bb09a 100644 --- a/src/libexpr/parser.y +++ b/src/libexpr/parser.y @@ -6,6 +6,7 @@ %parse-param { ParseData * data } %lex-param { yyscan_t scanner } + %{ #include <stdio.h> #include <stdlib.h> @@ -13,19 +14,21 @@ #include <aterm2.h> #include "parser-tab.hh" -extern "C" { -#include "lexer-tab.h" -} +#include "lexer-tab.hh" #include "aterm.hh" +#include "util.hh" #include "nixexpr.hh" #include "nixexpr-ast.hh" + using namespace nix; + namespace nix { - + + struct ParseData { Expr result; @@ -34,33 +37,52 @@ struct ParseData string error; }; -void setParseResult(ParseData * data, ATerm t); -void parseError(ParseData * data, char * error, int line, int column); -ATerm absParsedPath(ParseData * data, ATerm t); -ATerm fixAttrs(int recursive, ATermList as); -const char * getPath(ParseData * data); -Expr unescapeStr(const char * s); - -extern "C" { - void backToString(yyscan_t scanner); -} - -} -void yyerror(YYLTYPE * loc, yyscan_t scanner, ParseData * data, char * s) +static ATerm fixAttrs(int recursive, ATermList as) { - parseError(data, s, loc->first_line, loc->first_column); + ATermList bs = ATempty, cs = ATempty; + ATermList * is = recursive ? &cs : &bs; + for (ATermIterator i(as); i; ++i) { + ATermList names; + Expr src; + ATerm pos; + if (matchInherit(*i, src, names, pos)) { + bool fromScope = matchScope(src); + for (ATermIterator j(names); j; ++j) { + Expr rhs = fromScope ? makeVar(*j) : makeSelect(src, *j); + *is = ATinsert(*is, makeBind(*j, rhs, pos)); + } + } else bs = ATinsert(bs, *i); + } + if (recursive) + return makeRec(bs, cs); + else + return makeAttrs(bs); } + +void backToString(yyscan_t scanner); + + static Pos makeCurPos(YYLTYPE * loc, ParseData * data) { - return makePos(toATerm(getPath(data)), + return makePos(toATerm(data->path), loc->first_line, loc->first_column); } #define CUR_POS makeCurPos(yylocp, data) +} + + +void yyerror(YYLTYPE * loc, yyscan_t scanner, ParseData * data, char * error) +{ + data->error = (format("%1%, at `%2%':%3%:%4%") + % error % data->path % loc->first_line % loc->first_column).str(); +} + + /* Make sure that the parse stack is scanned by the ATerm garbage collector. */ static void * mallocAndProtect(size_t size) @@ -107,7 +129,7 @@ static void freeAndUnprotect(void * p) %% -start: expr { setParseResult(data, $1); }; +start: expr { data->result = $1; }; expr: expr_function; @@ -165,7 +187,7 @@ expr_simple else if (ATgetNext($2) == ATempty) $$ = ATgetFirst($2); else $$ = makeConcatStrings(ATreverse($2)); } - | PATH { $$ = makePath(absParsedPath(data, $1)); } + | PATH { $$ = makePath(toATerm(absPath(aterm2String($1), data->basePath))); } | URI { $$ = makeUri($1); } | '(' expr ')' { $$ = $2; } /* Let expressions `let {..., body = ...}' are just desugared @@ -224,3 +246,151 @@ formal ; %% + + +#include "eval.hh" + +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> + + +namespace nix { + + +static void checkAttrs(ATermMap & names, ATermList bnds) +{ + for (ATermIterator i(bnds); i; ++i) { + ATerm name; + Expr e; + ATerm pos; + if (!matchBind(*i, name, e, pos)) abort(); /* can't happen */ + if (names.get(name)) + throw EvalError(format("duplicate attribute `%1%' at %2%") + % aterm2String(name) % showPos(pos)); + names.set(name, name); + } +} + + +static void checkAttrSets(ATerm e) +{ + ATermList formals; + ATerm body, pos; + if (matchFunction(e, formals, body, pos)) { + ATermMap names(ATgetLength(formals)); + for (ATermIterator i(formals); i; ++i) { + ATerm name; + ATerm d1, d2; + if (!matchFormal(*i, name, d1, d2)) abort(); + if (names.get(name)) + throw EvalError(format("duplicate formal function argument `%1%' at %2%") + % aterm2String(name) % showPos(pos)); + names.set(name, name); + } + } + + ATermList bnds; + if (matchAttrs(e, bnds)) { + ATermMap names(ATgetLength(bnds)); + checkAttrs(names, bnds); + } + + ATermList rbnds, nrbnds; + if (matchRec(e, rbnds, nrbnds)) { + ATermMap names(ATgetLength(rbnds) + ATgetLength(nrbnds)); + checkAttrs(names, rbnds); + checkAttrs(names, nrbnds); + } + + if (ATgetType(e) == AT_APPL) { + int arity = ATgetArity(ATgetAFun(e)); + for (int i = 0; i < arity; ++i) + checkAttrSets(ATgetArgument(e, i)); + } + + else if (ATgetType(e) == AT_LIST) + for (ATermIterator i((ATermList) e); i; ++i) + checkAttrSets(*i); +} + + +static Expr parse(EvalState & state, + const char * text, const Path & path, + const Path & basePath) +{ + yyscan_t scanner; + ParseData data; + data.basePath = basePath; + data.path = path; + + yylex_init(&scanner); + yy_scan_string(text, scanner); + int res = yyparse(scanner, &data); + yylex_destroy(scanner); + + if (res) throw EvalError(data.error); + + try { + checkVarDefs(state.primOps, data.result); + } catch (Error & e) { + throw EvalError(format("%1%, in `%2%'") % e.msg() % path); + } + + checkAttrSets(data.result); + + return data.result; +} + + +Expr parseExprFromFile(EvalState & state, Path path) +{ + SwitchToOriginalUser sw; + + assert(path[0] == '/'); + +#if 0 + /* Perhaps this is already an imploded parse tree? */ + Expr e = ATreadFromNamedFile(path.c_str()); + if (e) return e; +#endif + + /* If `path' is a symlink, follow it. This is so that relative + path references work. */ + struct stat st; + if (lstat(path.c_str(), &st)) + throw SysError(format("getting status of `%1%'") % path); + if (S_ISLNK(st.st_mode)) path = absPath(readLink(path), dirOf(path)); + + /* If `path' refers to a directory, append `/default.nix'. */ + if (stat(path.c_str(), &st)) + throw SysError(format("getting status of `%1%'") % path); + if (S_ISDIR(st.st_mode)) + path = canonPath(path + "/default.nix"); + + /* Read the input file. We can't use SGparseFile() because it's + broken, so we read the input ourselves and call + SGparseString(). */ + AutoCloseFD fd = open(path.c_str(), O_RDONLY); + if (fd == -1) throw SysError(format("opening `%1%'") % path); + + if (fstat(fd, &st) == -1) + throw SysError(format("statting `%1%'") % path); + + char text[st.st_size + 1]; + readFull(fd, (unsigned char *) text, st.st_size); + text[st.st_size] = 0; + + return parse(state, text, path, dirOf(path)); +} + + +Expr parseExprFromString(EvalState & state, + const string & s, const Path & basePath) +{ + return parse(state, s.c_str(), "(string)", basePath); +} + + +} |