Permalink
Browse files

fixed calculations and //keyword

  • Loading branch information...
1 parent 2cb3f54 commit 73db545d617664f06b408bf8d4ece9f8488f68f7 Kyle Maxwell committed Apr 1, 2009
Showing with 2,179 additions and 1,803 deletions.
  1. +42 −14 parsed_xpath.c
  2. +5 −1 parsed_xpath.h
  3. +1,044 −877 parser.c
  4. +8 −5 parser.h
  5. +27 −23 parser.y
  6. +1 −1 test/div.json
  7. +1,044 −877 y.tab.c
  8. +8 −5 y.tab.h
View
56 parsed_xpath.c
@@ -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++) {
@@ -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;
-}
+}
View
6 parsed_xpath.h
@@ -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);
View
1,921 parser.c
1,044 additions, 877 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
13 parser.h
@@ -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))
@@ -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
View
50 parser.y
@@ -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))
@@ -303,7 +306,7 @@ Expr
PrimaryExpr
: VariableReference
| LPAREN Expr RPAREN { $$ = PXPWRAP($1, $2, $3); }
- | Literal { $$ = PXP($1); }
+ | Literal { $$ = LIT($1); }
| Number
| FunctionCall
;
@@ -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); }
@@ -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
@@ -439,6 +442,7 @@ LocalPart
NCName
: NAME
+ | keyword
;
selectors_group
View
2 test/div.json
@@ -1 +1 @@
-{ "div": "Web " }
+{ "div": "WebImagesMapsNewsVideoGmailmore »", "a": "Images", "calc1": "Google", "calc2": "2", "calc3": "4", "calc4": "3", "prolly-nan": "NaN" }
View
1,921 y.tab.c
1,044 additions, 877 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
13 y.tab.h
@@ -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))
@@ -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 "y.tab.h"
+/* Line 2616 of glr.c. */
+#line 202 "y.tab.h"
YYSTYPE;
# define YYSTYPE_IS_DECLARED 1
# define YYSTYPE_IS_TRIVIAL 1

0 comments on commit 73db545

Please sign in to comment.