19 action pkgstart { ts = p; }
22 inapt_action *tmp_action = new inapt_action;
23 tmp_action->package = xstrndup(ts, p - ts);
24 tmp_action->action = curaction;
25 tmp_action->linenum = curline;
26 tmp_action->filename = curfile;
27 block_stack.back()->actions.push_back(tmp_action);
31 curaction = inapt_action::INSTALL;
35 curaction = inapt_action::REMOVE;
44 inapt_block *tmp_block = new inapt_block;
45 block_stack.push_back(tmp_block);
48 fatal("%s: %d: Syntax Error: Nesting Too Deep at '}'", curfile, curline);
56 fatal("%s: %d: Syntax Error: Unexpected '}'", curfile, curline);
60 action start_conditional {
61 inapt_conditional *cond = new inapt_conditional;
62 cond->condition = xstrndup(ts, p - ts);
63 conditional_stack.push_back(cond);
66 action full_conditional {
67 inapt_conditional *cond = conditional_stack.back(); conditional_stack.pop_back();
68 cond->else_block = block_stack.back(); block_stack.pop_back();
69 cond->then_block = block_stack.back(); block_stack.pop_back();
70 block_stack.back()->children.push_back(cond);
73 action half_conditional {
74 inapt_conditional *cond = conditional_stack.back(); conditional_stack.pop_back();
75 cond->else_block = NULL;
76 cond->then_block = block_stack.back(); block_stack.pop_back();
77 block_stack.back()->children.push_back(cond);
80 newline = '\n' @newline;
81 comment = '#' (any - '\n')* newline;
82 whitespace = [\t\v\f\r ] | comment | newline;
83 package_name = ((lower | digit) (lower | digit | '+' | '-' | '.')+) >pkgstart;
84 package_list = ((whitespace+ package_name)+ %add_list whitespace*);
85 cmd_install = ('install' @install package_list ';');
86 cmd_remove = ('remove' @remove package_list ';');
87 simple_cmd = cmd_install | cmd_remove;
88 start_block = '{' @start_block;
89 end_block = '}' @end_block;
90 cmd_if = 'if' whitespace+ alpha+ >pkgstart %start_conditional whitespace* start_block whitespace*
91 ('else' whitespace* start_block whitespace* ';' @full_conditional | ';' @half_conditional);
92 cmd_list = (simple_cmd | cmd_if | whitespace)* end_block?;
100 void badsyntax(const char *filename, int lineno, char badchar, const char *message) {
103 message = "Unexpected newline";
104 else if (isspace(badchar))
105 message = "Unexpected whitespace";
107 message = "Syntax error";
110 if (isprint(badchar) && !isspace(badchar))
111 fatal("%s: %d: %s at '%c'", filename, lineno, message, badchar);
113 fatal("%s: %d: %s", filename, lineno, message);
116 void parser(const char *filename, inapt_block *top_block)
118 static char buf[BUFSIZE];
125 std::vector<inapt_block *> block_stack;
126 std::vector<inapt_conditional *> conditional_stack;
127 block_stack.push_back(top_block);
132 const char *curfile = filename;
133 enum inapt_action::action_t curaction = inapt_action::UNSET;
136 fd = open(filename, O_RDONLY);
138 fatalpe("open: %s", filename);
147 char *p = buf + have, *pe, *eof = 0;
148 int len, space = BUFSIZE - have;
151 fprintf(stderr, "OUT OF BUFFER SPACE\n");
155 len = read(fd, p, space);
157 fprintf(stderr, "IO ERROR\n");
169 if (cs == inapt_error)
170 badsyntax(curfile, curline, *p, NULL);
176 memmove(buf, ts, have);
181 if (cs < inapt_first_final) {
182 fprintf(stderr, "UNEXPECTED EOF\n");
187 badsyntax(curfile, curline, 0, "Unclosed block at EOF");