Skip to content

Commit

Permalink
fixed calculations and //keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Maxwell committed Apr 1, 2009
1 parent 2cb3f54 commit 73db545
Show file tree
Hide file tree
Showing 8 changed files with 2,179 additions and 1,803 deletions.
56 changes: 42 additions & 14 deletions parsed_xpath.c
Expand Up @@ -94,10 +94,8 @@ pxpathPtr pxpath_new_func(char* value, pxpathPtr child) {
return ptr;
}

pxpathPtr pxpath_cat_paths(int n, ...) {
pxpathPtr pxpath_cat_things(int n, va_list va) {
struct printbuf *buf = printbuf_new();
va_list va;
va_start(va, n);
pxpathPtr last;
pxpathPtr first;
for(int i = 0; i < n; i++) {
Expand All @@ -113,28 +111,58 @@ pxpathPtr pxpath_cat_paths(int n, ...) {
return out;
}

pxpathPtr pxpath_new_path(int n, ...) {
struct printbuf *buf = printbuf_new();
va_list va;
pxpathPtr pxpath_cat_literals(int n, ...) {
va_list va;
va_start(va, n);
pxpathPtr out = pxpath_cat_things(n, va);
va_end(va);
out->type = PXPATH_LIT_EXPR;
return out;
}

pxpathPtr pxpath_cat_paths(int n, ...) {
va_list va;
va_start(va, n);
pxpathPtr out = pxpath_cat_things(n, va);
va_end(va);
out->type = PXPATH_PATH;
return out;
}

static pxpathPtr
pxpath_new_thing(int n, va_list va) {
struct printbuf *buf = printbuf_new();
for(int i = 0; i < n; i++) {
sprintbuf(buf, "%s", va_arg(va, char *));
}
va_end(va);
pxpathPtr ptr = pxpath_new(PXPATH_PATH, buf->buf);
printbuf_free(buf);
return ptr;
}

pxpathPtr pxpath_new_path(int n, ...) {
va_list va;
va_start(va, n);
pxpathPtr ptr = pxpath_new_thing(n, va);
va_end(va);
ptr->type = PXPATH_PATH;
return ptr;
}

pxpathPtr pxpath_new_literal(int n, ...) {
struct printbuf *buf = printbuf_new();
va_list va;
va_start(va, n);
for(int i = 0; i < n; i++) {
sprintbuf(buf, "%s", va_arg(va, char *));
}
pxpathPtr ptr = pxpath_new_thing(n, va);
va_end(va);
pxpathPtr ptr = pxpath_new(PXPATH_LITERAL, buf->buf);
printbuf_free(buf);
ptr->type = PXPATH_LITERAL;
return ptr;
}

pxpathPtr pxpath_new_operator(int n, ...) {
va_list va;
va_start(va, n);
pxpathPtr ptr = pxpath_new_thing(n, va);
va_end(va);
ptr->type = PXPATH_OPERATOR;
return ptr;
}
}
6 changes: 5 additions & 1 deletion parsed_xpath.h
Expand Up @@ -15,15 +15,19 @@ typedef pxpath_node * pxpathPtr;
enum {
PXPATH_FUNCTION = 1,
PXPATH_PATH = 2,
PXPATH_LITERAL = 3
PXPATH_LITERAL = 3,
PXPATH_LIT_EXPR = 4,
PXPATH_OPERATOR = 3
};

pxpathPtr pxpath_new(int type, char* value);
pxpathPtr pxpath_new_func(char* value, pxpathPtr child);
pxpathPtr pxpath_cat_paths(int n, ...);
pxpathPtr pxpath_cat_literals(int n, ...);
pxpathPtr pxpath_new_path(int n, ...);
pxpathPtr pxpath_dup(pxpathPtr p);
pxpathPtr pxpath_new_literal(int n, ...);
pxpathPtr pxpath_new_operator(int n, ...);
void pxpath_free(pxpathPtr ptr);
char* pxpath_to_string(pxpathPtr ptr);

Expand Down
1,921 changes: 1,044 additions & 877 deletions parser.c

Large diffs are not rendered by default.

13 changes: 8 additions & 5 deletions parser.h
Expand Up @@ -170,9 +170,12 @@ int yyparse(void);
pxpathPtr myparse(char*);
void answer(pxpathPtr);

#define BIN_OP(A, B, C) pxpath_cat_paths(3, A, PXP(B), C);
#define PREP_OP(A, B) pxpath_cat_paths(2, PXP(A), B)
#define LIT_BIN_OP(A, B, C) pxpath_cat_literals(3, A, LIT(B), C)
#define BIN_OP(A, B, C) pxpath_cat_paths(3, A, OP(B), C)
#define PREP_OP(A, B) pxpath_cat_paths(2, OP(A), B)
#define PXP(A) pxpath_new_path(1, A)
#define LIT(A) pxpath_new_literal(1, A)
#define OP(A) pxpath_new_operator(1, A)
#define APPEND(A, S) pxpath_cat_paths(2, A, PXP(S));
#define PREPEND(A, S) pxpath_cat_paths(2, PXP(S), A);
#define PXPWRAP(A, B, C) pxpath_cat_paths(3, PXP(A), B, PXP(C))
Expand All @@ -188,14 +191,14 @@ void answer(pxpathPtr);

#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
typedef union YYSTYPE
#line 50 "parser.y"
#line 53 "parser.y"
{
int empty;
char* string;
pxpathPtr node;
}
/* Line 2604 of glr.c. */
#line 199 "parser.h"
/* Line 2616 of glr.c. */
#line 202 "parser.h"
YYSTYPE;
# define YYSTYPE_IS_DECLARED 1
# define YYSTYPE_IS_TRIVIAL 1
Expand Down
50 changes: 27 additions & 23 deletions parser.y
Expand Up @@ -27,9 +27,12 @@ int yyparse(void);
pxpathPtr myparse(char*);
void answer(pxpathPtr);

#define BIN_OP(A, B, C) pxpath_cat_paths(3, A, PXP(B), C);
#define PREP_OP(A, B) pxpath_cat_paths(2, PXP(A), B)
#define LIT_BIN_OP(A, B, C) pxpath_cat_literals(3, A, LIT(B), C)
#define BIN_OP(A, B, C) pxpath_cat_paths(3, A, OP(B), C)
#define PREP_OP(A, B) pxpath_cat_paths(2, OP(A), B)
#define PXP(A) pxpath_new_path(1, A)
#define LIT(A) pxpath_new_literal(1, A)
#define OP(A) pxpath_new_operator(1, A)
#define APPEND(A, S) pxpath_cat_paths(2, A, PXP(S));
#define PREPEND(A, S) pxpath_cat_paths(2, PXP(S), A);
#define PXPWRAP(A, B, C) pxpath_cat_paths(3, PXP(A), B, PXP(C))
Expand Down Expand Up @@ -303,7 +306,7 @@ Expr
PrimaryExpr
: VariableReference
| LPAREN Expr RPAREN { $$ = PXPWRAP($1, $2, $3); }
| Literal { $$ = PXP($1); }
| Literal { $$ = LIT($1); }
| Number
| FunctionCall
;
Expand Down Expand Up @@ -341,41 +344,41 @@ FilterExpr

OrExpr
: AndExpr
| OrExpr XOR AndExpr { $$ = BIN_OP($1, $2, $3); }
| OrExpr XOR AndExpr { $$ = LIT_BIN_OP($1, $2, $3); }
;

AndExpr
: EqualityExpr
| AndExpr XAND EqualityExpr { $$ = BIN_OP($1, $2, $3); }
| AndExpr XAND EqualityExpr { $$ = LIT_BIN_OP($1, $2, $3); }
;

EqualityExpr
: RelationalExpr
| EqualityExpr EQ RelationalExpr { $$ = BIN_OP($1, $2, $3); }
| EqualityExpr CXOPNE RelationalExpr { $$ = BIN_OP($1, $2, $3); }
| EqualityExpr EQ RelationalExpr { $$ = LIT_BIN_OP($1, $2, $3); }
| EqualityExpr CXOPNE RelationalExpr { $$ = LIT_BIN_OP($1, $2, $3); }
;

RelationalExpr
: AdditiveExpr
| RelationalExpr LT AdditiveExpr { $$ = BIN_OP($1, $2, $3); }
| RelationalExpr GT AdditiveExpr { $$ = BIN_OP($1, $2, $3); }
| RelationalExpr LTE AdditiveExpr { $$ = BIN_OP($1, $2, $3); }
| RelationalExpr GTE AdditiveExpr { $$ = BIN_OP($1, $2, $3); }
| RelationalExpr OptS LT OptS AdditiveExpr { $$ = LIT_BIN_OP($1, $3, $5); }
| RelationalExpr OptS GT OptS AdditiveExpr { $$ = LIT_BIN_OP($1, $3, $5); }
| RelationalExpr OptS LTE OptS AdditiveExpr { $$ = LIT_BIN_OP($1, $3, $5); }
| RelationalExpr OptS GTE OptS AdditiveExpr { $$ = LIT_BIN_OP($1, $3, $5); }
;

AdditiveExpr
: MultiplicativeExpr
| AdditiveExpr PLUS MultiplicativeExpr { $$ = BIN_OP($1, $2, $3); }
| AdditiveExpr DASH MultiplicativeExpr { $$ = BIN_OP($1, $2, $3); }
| AdditiveExpr OptS PLUS OptS MultiplicativeExpr { $$ = LIT_BIN_OP($1, $3, $5); }
| AdditiveExpr OptS DASH OptS MultiplicativeExpr { $$ = LIT_BIN_OP($1, $3, $5); }
;

MultiplicativeExpr
: UnaryExpr
| MultiplicativeExpr MultiplyOperator UnaryExpr { $$ = BIN_OP($1, $2, $3); }
| MultiplicativeExpr XDIV UnaryExpr { $$ = BIN_OP($1, $2, $3); }
| MultiplicativeExpr XMOD UnaryExpr { $$ = BIN_OP($1, $2, $3); }
| MultiplicativeExpr OptS MultiplyOperator OptS UnaryExpr { $$ = LIT_BIN_OP($1, $3, $5); }
| MultiplicativeExpr OptS XDIV OptS UnaryExpr { $$ = LIT_BIN_OP($1, $3, $5); }
| MultiplicativeExpr OptS XMOD OptS UnaryExpr { $$ = LIT_BIN_OP($1, $3, $5); }
;

UnaryExpr
: UnionExpr
| DASH UnaryExpr { $$ = PREP_OP($1, $2); }
Expand All @@ -385,10 +388,10 @@ Literal
: STRING
;
Number
: NUMBER { $$ = PXP($1); }
| NUMBER DOT { $$ = pxpath_new_path(2, $1, $2); }
| NUMBER DOT NUMBER { $$ = pxpath_new_path(3, $1, $2, $3); }
| DOT NUMBER { $$ = pxpath_new_path(2, $1, $2); }
: NUMBER { $$ = LIT($1); }
| NUMBER DOT { $$ = pxpath_new_literal(2, $1, $2); }
| NUMBER DOT NUMBER { $$ = pxpath_new_literal(3, $1, $2, $3); }
| DOT NUMBER { $$ = pxpath_new_literal(2, $1, $2); }
;

MultiplyOperator
Expand Down Expand Up @@ -439,6 +442,7 @@ LocalPart

NCName
: NAME
| keyword
;

selectors_group
Expand Down
2 changes: 1 addition & 1 deletion test/div.json
@@ -1 +1 @@
{ "div": "Web " }
{ "div": "WebImagesMapsNewsVideoGmailmore »", "a": "Images", "calc1": "Google", "calc2": "2", "calc3": "4", "calc4": "3", "prolly-nan": "NaN" }

0 comments on commit 73db545

Please sign in to comment.