1 /* 2 * Copyright (C) 2013-2014 Jo-Philipp Wich <jo@mein.io> 3 * 4 * Permission to use, copy, modify, and/or distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 #include "ast.h" 18 #include "lexer.h" 19 20 #include <stdio.h> 21 #include <stdlib.h> 22 #include <stdarg.h> 23 #include <string.h> 24 #include <libubox/utils.h> 25 26 struct jp_opcode * 27 jp_alloc_op(struct jp_state *s, int type, int num, char *str, ...) 28 { 29 va_list ap; 30 char *ptr; 31 struct jp_opcode *newop, *child; 32 33 newop = calloc_a(sizeof(*newop), 34 str ? &ptr : NULL, str ? strlen(str) + 1 : 0); 35 36 if (!newop) 37 { 38 fprintf(stderr, "Out of memory\n"); 39 exit(127); 40 } 41 42 newop->type = type; 43 newop->num = num; 44 45 if (str) 46 newop->str = strcpy(ptr, str); 47 48 va_start(ap, str); 49 50 while ((child = va_arg(ap, void *)) != NULL) 51 if (!newop->down) 52 newop->down = child; 53 else 54 append_op(newop->down, child); 55 56 va_end(ap); 57 58 newop->next = s->pool; 59 s->pool = newop; 60 61 return newop; 62 } 63 64 void 65 jp_free(struct jp_state *s) 66 { 67 struct jp_opcode *op, *tmp; 68 69 for (op = s->pool; op;) 70 { 71 tmp = op->next; 72 free(op); 73 op = tmp; 74 } 75 76 free(s); 77 } 78 79 struct jp_state * 80 jp_parse(const char *expr) 81 { 82 struct jp_state *s; 83 struct jp_opcode *op; 84 const char *ptr = expr; 85 void *pParser; 86 int len = strlen(expr); 87 int mlen = 0; 88 89 s = calloc(1, sizeof(*s)); 90 91 if (!s) 92 return NULL; 93 94 pParser = ParseAlloc(malloc); 95 96 if (!pParser) 97 return NULL; 98 99 while (len > 0) 100 { 101 op = jp_get_token(s, ptr, &mlen); 102 103 if (mlen < 0) 104 { 105 s->error_code = mlen; 106 goto out; 107 } 108 109 if (op) 110 Parse(pParser, op->type, op, s); 111 112 len -= mlen; 113 ptr += mlen; 114 115 s->off += mlen; 116 } 117 118 Parse(pParser, 0, NULL, s); 119 120 out: 121 ParseFree(pParser, free); 122 123 return s; 124 } 125
This page was automatically generated by LXR 0.3.1. • OpenWrt