* Use a proper namespace.
* Optimise header file usage a bit. * Compile the parser as C++.
This commit is contained in:
		
							parent
							
								
									aab8812732
								
							
						
					
					
						commit
						75068e7d75
					
				
					 61 changed files with 650 additions and 268 deletions
				
			
		|  | @ -2,13 +2,13 @@ pkglib_LTLIBRARIES = libexpr.la | |||
| 
 | ||||
| libexpr_la_SOURCES = nixexpr.cc nixexpr.hh parser.cc parser.hh \ | ||||
|  eval.cc eval.hh primops.cc \ | ||||
|  lexer-tab.c lexer-tab.h parser-tab.c parser-tab.h \ | ||||
|  lexer-tab.c lexer-tab.h 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.h lexer-tab.h parser-tab.c lexer-tab.c | ||||
|  parser-tab.hh lexer-tab.h parser-tab.cc lexer-tab.c | ||||
| 
 | ||||
| EXTRA_DIST = lexer.l parser.y nixexpr-ast.def nixexpr-ast.cc | ||||
| 
 | ||||
|  | @ -21,8 +21,8 @@ AM_CFLAGS = \ | |||
| 
 | ||||
| # Parser generation. | ||||
| 
 | ||||
| parser-tab.c parser-tab.h: parser.y | ||||
| 	$(bison) -v -o parser-tab.c $(srcdir)/parser.y -d | ||||
| 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  | ||||
|  |  | |||
|  | @ -1,5 +1,9 @@ | |||
| #include "attr-path.hh" | ||||
| #include "nixexpr-ast.hh" | ||||
| #include "util.hh" | ||||
| 
 | ||||
| 
 | ||||
| namespace nix { | ||||
| 
 | ||||
| 
 | ||||
| bool isAttrs(EvalState & state, Expr e, ATermMap & attrs) | ||||
|  | @ -73,3 +77,6 @@ Expr findAlongAttrPath(EvalState & state, const string & attrPath, | |||
|      | ||||
|     return e; | ||||
| } | ||||
| 
 | ||||
|   | ||||
| } | ||||
|  |  | |||
|  | @ -7,8 +7,14 @@ | |||
| #include "eval.hh" | ||||
| 
 | ||||
| 
 | ||||
| namespace nix { | ||||
| 
 | ||||
|      | ||||
| Expr findAlongAttrPath(EvalState & state, const string & attrPath, | ||||
|     const ATermMap & autoArgs, Expr e); | ||||
| 
 | ||||
|      | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| #endif /* !__ATTR_PATH_H */ | ||||
|  |  | |||
|  | @ -1,8 +1,13 @@ | |||
| #include "eval.hh" | ||||
| #include "parser.hh" | ||||
| #include "hash.hh" | ||||
| #include "util.hh" | ||||
| #include "nixexpr-ast.hh" | ||||
| 
 | ||||
| 
 | ||||
| namespace nix { | ||||
|      | ||||
| 
 | ||||
| EvalState::EvalState() | ||||
|     : normalForms(32768), primOps(128) | ||||
| { | ||||
|  | @ -271,7 +276,7 @@ Expr wrapInContext(ATermList context, Expr e) | |||
| static ATerm concatStrings(EvalState & state, const ATermVector & args) | ||||
| { | ||||
|     ATermList context = ATempty; | ||||
|     ostringstream s; | ||||
|     std::ostringstream s; | ||||
|     bool isPath = false; | ||||
| 
 | ||||
|     for (ATermVector::const_iterator i = args.begin(); i != args.end(); ++i) { | ||||
|  | @ -666,3 +671,6 @@ void printEvalStats(EvalState & state) | |||
|     if (showStats) | ||||
|         printATermMapStats(); | ||||
| } | ||||
| 
 | ||||
|   | ||||
| } | ||||
|  |  | |||
|  | @ -4,16 +4,21 @@ | |||
| #include <map> | ||||
| 
 | ||||
| #include "aterm.hh" | ||||
| #include "hash.hh" | ||||
| #include "nixexpr.hh" | ||||
| 
 | ||||
| 
 | ||||
| typedef map<Path, PathSet> DrvRoots; | ||||
| typedef map<Path, Hash> DrvHashes; | ||||
| namespace nix { | ||||
| 
 | ||||
| 
 | ||||
| class Hash; | ||||
|      | ||||
| 
 | ||||
| typedef std::map<Path, PathSet> DrvRoots; | ||||
| typedef std::map<Path, Hash> DrvHashes; | ||||
| 
 | ||||
| /* Cache for calls to addToStore(); maps source paths to the store
 | ||||
|    paths. */ | ||||
| typedef map<Path, Path> SrcToStore; | ||||
| typedef std::map<Path, Path> SrcToStore; | ||||
| 
 | ||||
| struct EvalState; | ||||
| 
 | ||||
|  | @ -74,5 +79,8 @@ Expr autoCallFunction(Expr e, const ATermMap & args); | |||
| /* Print statistics. */ | ||||
| void printEvalStats(EvalState & state); | ||||
| 
 | ||||
|   | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| #endif /* !__EVAL_H */ | ||||
|  |  | |||
|  | @ -1,10 +1,12 @@ | |||
| #include "expr-to-xml.hh" | ||||
| 
 | ||||
| #include "xml-writer.hh" | ||||
| #include "nixexpr-ast.hh" | ||||
| #include "aterm.hh" | ||||
| 
 | ||||
| 
 | ||||
| namespace nix { | ||||
| 
 | ||||
|      | ||||
| static XMLAttrs singletonAttrs(const string & name, const string & value) | ||||
| { | ||||
|     XMLAttrs attrs; | ||||
|  | @ -84,9 +86,12 @@ static void printTermAsXML(Expr e, XMLWriter & doc) | |||
| } | ||||
| 
 | ||||
| 
 | ||||
| void printTermAsXML(Expr e, ostream & out) | ||||
| void printTermAsXML(Expr e, std::ostream & out) | ||||
| { | ||||
|     XMLWriter doc(true, out); | ||||
|     XMLOpenElement root(doc, "expr"); | ||||
|     printTermAsXML(e, doc); | ||||
| } | ||||
| 
 | ||||
|   | ||||
| } | ||||
|  |  | |||
|  | @ -6,8 +6,10 @@ | |||
| 
 | ||||
| #include "nixexpr.hh" | ||||
| 
 | ||||
| namespace nix { | ||||
| 
 | ||||
| void printTermAsXML(Expr e, ostream & out); | ||||
| 
 | ||||
| void printTermAsXML(Expr e, std::ostream & out); | ||||
|      | ||||
| } | ||||
| 
 | ||||
| #endif /* !__EXPR_TO_XML_H */ | ||||
|  |  | |||
|  | @ -1,5 +1,9 @@ | |||
| #include "get-drvs.hh" | ||||
| #include "nixexpr-ast.hh" | ||||
| #include "util.hh" | ||||
| 
 | ||||
| 
 | ||||
| namespace nix { | ||||
| 
 | ||||
| 
 | ||||
| string DrvInfo::queryDrvPath(EvalState & state) const | ||||
|  | @ -66,7 +70,7 @@ static bool getDerivation(EvalState & state, Expr e, | |||
|         e = evalExpr(state, e); | ||||
|         if (!matchAttrs(e, es)) return true; | ||||
| 
 | ||||
|         shared_ptr<ATermMap> attrs(new ATermMap(32)); /* !!! */ | ||||
|         boost::shared_ptr<ATermMap> attrs(new ATermMap(32)); /* !!! */ | ||||
|         queryAllAttrs(e, *attrs, false); | ||||
|      | ||||
|         Expr a = attrs->get(toATerm("type")); | ||||
|  | @ -183,3 +187,6 @@ void getDerivations(EvalState & state, Expr e, const string & pathPrefix, | |||
|     Exprs doneExprs; | ||||
|     getDerivations(state, e, pathPrefix, autoArgs, drvs, doneExprs); | ||||
| } | ||||
| 
 | ||||
|   | ||||
| } | ||||
|  |  | |||
|  | @ -9,7 +9,10 @@ | |||
| #include "eval.hh" | ||||
| 
 | ||||
| 
 | ||||
| typedef map<string, string> MetaInfo; | ||||
| namespace nix { | ||||
| 
 | ||||
| 
 | ||||
| typedef std::map<string, string> MetaInfo; | ||||
| 
 | ||||
| 
 | ||||
| struct DrvInfo | ||||
|  | @ -23,7 +26,7 @@ public: | |||
|     string attrPath; /* path towards the derivation */ | ||||
|     string system; | ||||
| 
 | ||||
|     shared_ptr<ATermMap> attrs; | ||||
|     boost::shared_ptr<ATermMap> attrs; | ||||
| 
 | ||||
|     string queryDrvPath(EvalState & state) const; | ||||
|     string queryOutPath(EvalState & state) const; | ||||
|  | @ -52,5 +55,8 @@ bool getDerivation(EvalState & state, Expr e, DrvInfo & drv); | |||
| void getDerivations(EvalState & state, Expr e, const string & pathPrefix, | ||||
|     const ATermMap & autoArgs, DrvInfos & drvs); | ||||
| 
 | ||||
|   | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| #endif /* !__GET_DRVS_H */ | ||||
|  |  | |||
|  | @ -9,7 +9,7 @@ | |||
| %{ | ||||
| #include <string.h> | ||||
| #include <aterm2.h> | ||||
| #include "parser-tab.h" | ||||
| #include "parser-tab.hh" | ||||
| 
 | ||||
| static void initLoc(YYLTYPE * loc) | ||||
| { | ||||
|  | @ -35,7 +35,11 @@ static void adjustLoc(YYLTYPE * loc, const char * s, size_t len) | |||
|     } | ||||
| } | ||||
| 
 | ||||
| ATerm toATerm(const char * s); | ||||
| ATerm toATerm(const char * s) | ||||
| { | ||||
|     return (ATerm) ATmakeAppl0(ATmakeAFun((char *) s, 0, ATtrue)); | ||||
| } | ||||
| 
 | ||||
| ATerm unescapeStr(const char * s); | ||||
| 
 | ||||
| #define YY_USER_INIT initLoc(yylloc) | ||||
|  |  | |||
|  | @ -1,11 +1,14 @@ | |||
| #include "nixexpr.hh" | ||||
| #include "derivations.hh" | ||||
| 
 | ||||
| #include "util.hh" | ||||
| 
 | ||||
| #include "nixexpr-ast.hh" | ||||
| #include "nixexpr-ast.cc" | ||||
| 
 | ||||
| 
 | ||||
| namespace nix { | ||||
|      | ||||
| 
 | ||||
| string showPos(ATerm pos) | ||||
| { | ||||
|     ATerm path; | ||||
|  | @ -332,3 +335,6 @@ string showValue(Expr e) | |||
|     /* !!! incomplete */ | ||||
|     return "<unknown>"; | ||||
| } | ||||
| 
 | ||||
|   | ||||
| } | ||||
|  |  | |||
|  | @ -6,7 +6,10 @@ | |||
| #include <aterm2.h> | ||||
| 
 | ||||
| #include "aterm-map.hh" | ||||
| #include "util.hh" | ||||
| #include "types.hh" | ||||
| 
 | ||||
| 
 | ||||
| namespace nix { | ||||
| 
 | ||||
| 
 | ||||
| MakeError(EvalError, Error) | ||||
|  | @ -95,5 +98,8 @@ string showType(Expr e); | |||
| 
 | ||||
| string showValue(Expr e); | ||||
| 
 | ||||
|   | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| #endif /* !__NIXEXPR_H */ | ||||
|  |  | |||
|  | @ -1,3 +1,8 @@ | |||
| #include "parser.hh" | ||||
| #include "aterm.hh" | ||||
| #include "util.hh" | ||||
| #include "nixexpr-ast.hh" | ||||
|      | ||||
| #include <sstream> | ||||
| 
 | ||||
| #include <sys/types.h> | ||||
|  | @ -5,9 +10,15 @@ | |||
| #include <fcntl.h> | ||||
| #include <unistd.h> | ||||
| 
 | ||||
| #include "aterm.hh" | ||||
| #include "parser.hh" | ||||
| #include "nixexpr-ast.hh" | ||||
| 
 | ||||
| extern "C" { | ||||
| 
 | ||||
| #include "parser-tab.hh" | ||||
| #include "lexer-tab.h" | ||||
|      | ||||
| } | ||||
| 
 | ||||
| namespace nix { | ||||
| 
 | ||||
| 
 | ||||
| struct ParseData  | ||||
|  | @ -17,16 +28,15 @@ struct ParseData | |||
|     Path path; | ||||
|     string error; | ||||
| }; | ||||
|   | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| extern "C" { | ||||
| int yyparse(yyscan_t scanner, nix::ParseData * data); | ||||
| 
 | ||||
| 
 | ||||
| namespace nix { | ||||
| 
 | ||||
| #include "parser-tab.h" | ||||
| #include "lexer-tab.h" | ||||
|      | ||||
| /* Callbacks for getting from C to C++.  Due to a (small) bug in the
 | ||||
|    GLR code of Bison we cannot currently compile the parser as C++ | ||||
|    code. */ | ||||
| 
 | ||||
| void setParseResult(ParseData * data, ATerm t) | ||||
| { | ||||
|  | @ -71,6 +81,7 @@ const char * getPath(ParseData * data) | |||
|     return data->path.c_str(); | ||||
| } | ||||
| 
 | ||||
| extern "C" { | ||||
| Expr unescapeStr(const char * s) | ||||
| { | ||||
|     string t; | ||||
|  | @ -93,11 +104,7 @@ Expr unescapeStr(const char * s) | |||
|     } | ||||
|     return makeStr(toATerm(t)); | ||||
| } | ||||
| 
 | ||||
| int yyparse(yyscan_t scanner, ParseData * data); | ||||
| 
 | ||||
| 
 | ||||
| } /* end of C functions */ | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| static void checkAttrs(ATermMap & names, ATermList bnds) | ||||
|  | @ -232,3 +239,6 @@ Expr parseExprFromString(EvalState & state, | |||
| { | ||||
|     return parse(state, s.c_str(), "(string)", basePath); | ||||
| } | ||||
| 
 | ||||
|   | ||||
| } | ||||
|  |  | |||
|  | @ -4,6 +4,9 @@ | |||
| #include "eval.hh" | ||||
| 
 | ||||
| 
 | ||||
| namespace nix { | ||||
| 
 | ||||
| 
 | ||||
| /* Parse a Nix expression from the specified file.  If `path' refers
 | ||||
|    to a directory, the "/default.nix" is appended. */ | ||||
| Expr parseExprFromFile(EvalState & state, Path path); | ||||
|  | @ -13,4 +16,7 @@ Expr parseExprFromString(EvalState & state, const string & s, | |||
|     const Path & basePath); | ||||
| 
 | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| #endif /* !__PARSER_H */ | ||||
|  |  | |||
|  | @ -3,7 +3,7 @@ | |||
| %locations | ||||
| %error-verbose | ||||
| %parse-param { yyscan_t scanner } | ||||
| %parse-param { void * data } | ||||
| %parse-param { ParseData * data } | ||||
| %lex-param { yyscan_t scanner } | ||||
| 
 | ||||
| %{ | ||||
|  | @ -12,34 +12,47 @@ | |||
| #include <string.h> | ||||
| #include <aterm2.h> | ||||
| 
 | ||||
| #include "parser-tab.h" | ||||
| #include "parser-tab.hh" | ||||
| extern "C" { | ||||
| #include "lexer-tab.h" | ||||
| } | ||||
| 
 | ||||
| typedef ATerm Expr; | ||||
| typedef ATerm ValidValues; | ||||
| typedef ATerm DefaultValue; | ||||
| typedef ATerm Pos; | ||||
| #include "aterm.hh" | ||||
|      | ||||
| #include "nixexpr.hh" | ||||
| #include "nixexpr-ast.hh" | ||||
| 
 | ||||
| void setParseResult(void * data, ATerm t); | ||||
| void parseError(void * data, char * error, int line, int column); | ||||
| ATerm absParsedPath(void * data, ATerm t); | ||||
| ATerm fixAttrs(int recursive, ATermList as); | ||||
| const char * getPath(void * data); | ||||
| void backToString(yyscan_t scanner); | ||||
| using namespace nix; | ||||
| 
 | ||||
| void yyerror(YYLTYPE * loc, yyscan_t scanner, void * data, char * s) | ||||
| namespace nix { | ||||
|   | ||||
| struct ParseData  | ||||
| { | ||||
|     Expr result; | ||||
|     Path basePath; | ||||
|     Path path; | ||||
|     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) | ||||
| { | ||||
|     parseError(data, s, loc->first_line, loc->first_column); | ||||
| } | ||||
| 
 | ||||
| ATerm toATerm(const char * s) | ||||
| { | ||||
|     return (ATerm) ATmakeAppl0(ATmakeAFun((char *) s, 0, ATtrue)); | ||||
| } | ||||
| 
 | ||||
| static Pos makeCurPos(YYLTYPE * loc, void * data) | ||||
| static Pos makeCurPos(YYLTYPE * loc, ParseData * data) | ||||
| { | ||||
|     return makePos(toATerm(getPath(data)), | ||||
|         loc->first_line, loc->first_column); | ||||
|  |  | |||
|  | @ -1,11 +1,16 @@ | |||
| #include <algorithm> | ||||
| 
 | ||||
| #include "build.hh" | ||||
| #include "misc.hh" | ||||
| #include "eval.hh" | ||||
| #include "globals.hh" | ||||
| #include "nixexpr-ast.hh" | ||||
| #include "store.hh" | ||||
| #include "util.hh" | ||||
| #include "expr-to-xml.hh" | ||||
| #include "nixexpr-ast.hh" | ||||
| 
 | ||||
| #include <algorithm> | ||||
| 
 | ||||
| 
 | ||||
| namespace nix { | ||||
| 
 | ||||
| 
 | ||||
| static Expr primBuiltins(EvalState & state, const ATermVector & args) | ||||
|  | @ -472,7 +477,7 @@ static Expr primToString(EvalState & state, const ATermVector & args) | |||
|    be sensibly or completely represented (e.g., functions). */ | ||||
| static Expr primToXML(EvalState & state, const ATermVector & args) | ||||
| { | ||||
|     ostringstream out; | ||||
|     std::ostringstream out; | ||||
|     printTermAsXML(strictEvalExpr(state, args[0]), out); | ||||
|     return makeStr(toATerm(out.str())); | ||||
| } | ||||
|  | @ -746,3 +751,6 @@ void EvalState::addPrimOps() | |||
|     addPrimOp("removeAttrs", 2, primRemoveAttrs); | ||||
|     addPrimOp("relativise", 2, primRelativise); | ||||
| } | ||||
| 
 | ||||
|   | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue