static struct AstNode *parse_if( struct DomNode *dom, struct ParserState *state) { struct DomNode *child = NULL; struct AstNode *test = NULL; struct AstNode *true_expr = NULL; struct AstNode *false_expr = NULL; /* 1. Is compound CORE */ if (!dom_node_is_spec_compound(dom, DOM_CPD_CORE)) { return NULL; } /* 2. Has 4 children */ if (!dom_node_is_cpd_of_size(dom, 4)) { return NULL; } child = dom->cpd_children; /* 3.1. 1st child is the if keyword. */ if (!dom_node_is_spec_reserved_atom(child, DOM_RES_IF)) { return NULL; } child = child->next; /* 3.2 2nd child is a valid expression. */ if (!(test = parse_one(child, state))) { return NULL; } child = child->next; /* 3.3 3rd child is a valid expression. */ if (!(true_expr = parse_one(child, state))) { ast_node_free(test); return NULL; } child = child->next; /* 3.4 4th child is a valid expression. */ if (!(false_expr = parse_one(child, state))) { ast_node_free(test); ast_node_free(true_expr); return NULL; } return ast_make_spec_if(test, true_expr, false_expr); }
bool isMatch_Helper(const char *s,const char *p,vector<vector<int> > table,int i, int j){ if(table[i][j] != -1 ) return (table[i][j] == 0)? false:true; if(p[j] == '\0') return (p[j] == s[i]); if(p[j] == '*'){ while(s[i] != '\0'){ if(isMatch_Helper(s,p,table,i,j+1)){ table[i][j] = 1; return 1; } i++; } while(p[j] != '\0'){ if(p[j] != '*'){ table[i][j] = 0; return 0; } j++; } table[i][j] = 1; } else return parse_one(s[i],p[j]) && isMatch_Helper(s,p,table,i+1,j+1); return table[i][j]; }
static struct AstNode *parse_binary( struct DomNode *dom, enum Reserved keyword, struct AstNode *(*constructor)( struct AstNode *, struct AstNode *), struct ParserState *state) { struct DomNode *child = NULL; struct AstNode *arg1 = NULL; struct AstNode *arg2 = NULL; /* 1. Is compound CORE */ if (!dom_node_is_spec_compound(dom, DOM_CPD_CORE)) { return NULL; } /* 2. Has 2 children */ if (!dom_node_is_cpd_of_size(dom, 3)) { return NULL; } child = dom->cpd_children; /* 2.1. 1st child is the proper keyword. */ if (!dom_node_is_spec_reserved_atom(child, keyword)) { return NULL; } child = child->next; /* 2.2 2nd child is a valid expression. */ if (!(arg1 = parse_one(child, state))) { return NULL; } child = child->next; /* 2.3 3rd child is a valid expression. */ if (!(arg2 = parse_one(child, state))) { ast_node_free(arg1); return NULL; } child = child->next; return constructor(arg1, arg2); }
/** * Manually sets the specified parameter. */ int param_set_by_name_int(char *param, int val) { struct kernel_param * params = __start___param; unsigned int num_params = __stop___param - __start___param; char valstr[128]; sprintf(valstr, "%d", val); return parse_one(param, valstr, params, num_params, NULL); }
static struct AstNode *parse_bind( struct DomNode *dom, struct ParserState *state) { struct DomNode *child = NULL; struct AstNode *pattern = NULL; struct AstNode *expr = NULL; /* 1. Is compound CORE */ if (!dom_node_is_spec_compound(dom, DOM_CPD_CORE)) { return NULL; } /* 2. Has 3 children */ if (!dom_node_is_cpd_of_size(dom, 3)) { return NULL; } child = dom->cpd_children; /* 2.1. 1st child is bind keyword. */ if (!dom_node_is_spec_reserved_atom(child, DOM_RES_BIND)) { return NULL; } child = child->next; /* 2.2. 2nd child is pattern. */ if (!(pattern = parse_one(child, state))) { return NULL; } child = child->next; /* 2.3 3rd child is any expression. */ if (!(expr = parse_one(child, state))) { ast_node_free(pattern); return NULL; } return ast_make_spec_bind(pattern, expr); }
/* Parse your arguments. */ bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...)) { int ret; unsigned offset = 0; /* This helps opt_usage. */ opt_argv0 = argv[0]; while ((ret = parse_one(argc, argv, &offset, errlog)) == 1); /* parse_one returns 0 on finish, -1 on error */ return (ret == 0); }
Atom* parse_string( SourceManager::Input& in ) { if( Atom* a = parse_one( in ) ) { in.eatwhite(); if( 0 ) if( in.peek() == ':' ) { in.next(); //Box rhs = parse_one( state, in ); //rhs->m_type = ret; //ret = rhs; } return a; } return nullptr; }
/* Args looks like "foo=bar,bar2 baz=fuz wiz". */ int parse_args(const char *name, char *args, struct kernel_param *params, unsigned num, int (*unknown)(char *param, char *val)) { char *param, *val; DEBUGP("Parsing ARGS: %s\n", args); /* Chew leading spaces */ while (*args == ' ') args++; while (*args) { int ret; int irq_was_disabled; args = next_arg(args, ¶m, &val); irq_was_disabled = irqs_disabled(); ret = parse_one(param, val, params, num, unknown); if (irq_was_disabled && !irqs_disabled()) { printk(KERN_WARNING "parse_args(): option '%s' enabled " "irq's!\n", param); } switch (ret) { case -ENOENT: printk(KERN_ERR "%s: Unknown parameter `%s'\n", name, param); return ret; case -ENOSPC: printk(KERN_ERR "%s: `%s' too large for parameter `%s'\n", name, val ?: "", param); return ret; case 0: break; default: printk(KERN_ERR "%s: `%s' invalid for parameter `%s'\n", name, val ?: "", param); return ret; } } /* All parsed OK. */ return 0; }
bool isMatch(const char *s,const char *p){ int i = 0; int j = 0; bool hasStar = false; while(j < strlen(p)){ while(i < strlen(s)){ if(p[j] == '*'){ hasStar = true; for(int k = 0; k <= strlen(s)-i;++k){ j++; while(i+k < strlen(s) && s[i+k] == p[j]){ i++; j++; } if(i+k == strlen(s)) return true; if(!hasStar) return false; } } else{ if(!parse_one(s[i],p[j])) return false; i++; j++; } } while(p[j] != '\0'){ if(p[j] != '*') return false; j++; } return j >= strlen(p); } return i >= strlen(s); }
bool isMatch(const char *s,const char *p){ if(*p == '\0') return (*p == *s); if(p[0] == '*'){ while(*s != '\0'){ if(isMatch(s,p+1)) return true; s++; } while(*p != '\0'){ if(*p != '*') return false; p++; } return true; } else return parse_one(s,p) && isMatch(s+1,p+1); }
static struct AstNode *parse_list( struct DomNode *dom, struct ParserState *state) { struct AstNode *node; struct AstNode *result = NULL; struct AstNode *result_end = NULL; while (dom) { if ((node = parse_one(dom, state))) { LIST_APPEND(node, &result, &result_end); } else { ast_node_free(result); return NULL; } dom = dom->next; } return result; }
static bool early_parse(int argc, char *argv[], void (*errlog)(const char *fmt, ...), bool ignore_unknown) { int ret; unsigned off = 0; char **tmpargv = opt_alloc.alloc(sizeof(argv[0]) * (argc + 1)); /* We could avoid a copy and skip instead, but this is simple. */ memcpy(tmpargv, argv, sizeof(argv[0]) * (argc + 1)); /* This helps opt_usage. */ opt_argv0 = argv[0]; while ((ret = parse_one(&argc, tmpargv, OPT_EARLY, &off, errlog, ignore_unknown)) == 1); opt_alloc.free(tmpargv); /* parse_one returns 0 on finish, -1 on error */ return (ret == 0); }
/* Parse your arguments. */ bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...)) { int ret; unsigned offset = 0; #ifdef WIN32 char *original_argv0 = argv[0]; argv[0] = (char*)basename(argv[0]); #endif /* This helps opt_usage. */ opt_argv0 = argv[0]; while ((ret = parse_one(argc, argv, &offset, errlog)) == 1); #ifdef WIN32 argv[0] = original_argv0; #endif /* parse_one returns 0 on finish, -1 on error */ return (ret == 0); }
/* Args looks like "foo=bar,bar2 baz=fuz wiz". */ int parse_args(const char *name, char *args, const struct kernel_param *params, unsigned num, int (*unknown)(char *param, char *val)) { char *param, *val; DEBUGP("Parsing ARGS: %s\n", args); /* Chew leading spaces */ args = skip_spaces(args); while (*args) { int ret; args = next_arg(args, ¶m, &val); ret = parse_one(param, val, params, num, unknown); switch (ret) { case -ENOENT: printk("ERROR: %s: Unknown parameter `%s'\n", name, param); return ret; case -ENOSPC: printk("ERROR: %s: `%s' too large for parameter `%s'\n", name, val ?: "", param); return ret; case 0: break; default: printk("ERROR: %s: `%s' invalid for parameter `%s'\n", name, val ?: "", param); return ret; } } /* All parsed OK. */ return 0; }
static struct AstNode *parse_func_call( struct DomNode *dom, struct ParserState *state) { struct AstNode *func = NULL; struct AstNode *args = NULL; struct DomNode *child = NULL; /* 1. Is compound CORE. */ if (!dom_node_is_spec_compound(dom, DOM_CPD_CORE)) { return NULL; } /* 2. Has 1 or more children. */ if (!dom_node_is_cpd_min_size(dom, 1)) { return NULL; } child = dom->cpd_children; /* 3.1. 1st child is an non-keyword expression. */ if (dom_node_is_reserved_atom(child)) { return NULL; } func = parse_one(child, state); if (err_state()) { return NULL; } child = child->next; /* 3.2. Has 0 or more further children being any expression. */ args = parse_list(child, state); if (err_state()) { return NULL; } return ast_make_func_call(func, args); }
SharedRefPtr<ParseResult> DataTypeClassNameParser::parse_with_composite(const std::string& type, const NativeDataTypes& native_types) { Parser parser(type, 0); std::string next; parser.get_next_name(&next); if (!is_composite(next)) { DataType::ConstPtr data_type = parse_one(type, native_types); if (!data_type) { return SharedRefPtr<ParseResult>(); } return SharedRefPtr<ParseResult>(new ParseResult(data_type, is_reversed(next))); } TypeParamsVec sub_class_names; if (!parser.get_type_params(&sub_class_names)) { return SharedRefPtr<ParseResult>(); } if (sub_class_names.empty()) { LOG_ERROR("Expected at least one subclass type for a composite type"); return SharedRefPtr<ParseResult>(); } ParseResult::CollectionMap collections; const std::string& last = sub_class_names.back(); size_t count = sub_class_names.size(); if (is_collection(last)) { count--; Parser collection_parser(last, 0); collection_parser.get_next_name(); NameAndTypeParamsVec params; if (!collection_parser.get_collection_params(¶ms)) { return SharedRefPtr<ParseResult>(); } for (NameAndTypeParamsVec::const_iterator i = params.begin(), end = params.end(); i != end; ++i) { DataType::ConstPtr data_type = parse_one(i->second, native_types); if (!data_type) { return SharedRefPtr<ParseResult>(); } collections[i->first] = data_type; } } DataType::Vec types; ParseResult::ReversedVec reversed; for (size_t i = 0; i < count; ++i) { DataType::ConstPtr data_type = parse_one(sub_class_names[i], native_types); if (!data_type) { return SharedRefPtr<ParseResult>(); } types.push_back(data_type); reversed.push_back(is_reversed(sub_class_names[i])); } return SharedRefPtr<ParseResult>(new ParseResult(true, types, reversed, collections)); }
DataType::ConstPtr DataTypeClassNameParser::parse_one(const std::string& type, const NativeDataTypes& native_types) { bool is_frozen = DataTypeClassNameParser::is_frozen(type); std::string class_name; if (is_reversed(type) || is_frozen) { if (!get_nested_class_name(type, &class_name)) { return DataType::ConstPtr(); } } else { class_name = type; } Parser parser(class_name, 0); std::string next; parser.get_next_name(&next); if (starts_with(next, LIST_TYPE)) { TypeParamsVec params; if (!parser.get_type_params(¶ms) || params.empty()) { return DataType::ConstPtr(); } DataType::ConstPtr element_type(parse_one(params[0], native_types)); if (!element_type) { return DataType::ConstPtr(); } return CollectionType::list(element_type, is_frozen); } else if(starts_with(next, SET_TYPE)) { TypeParamsVec params; if (!parser.get_type_params(¶ms) || params.empty()) { return DataType::ConstPtr(); } DataType::ConstPtr element_type(parse_one(params[0], native_types)); if (!element_type) { return DataType::ConstPtr(); } return CollectionType::set(element_type, is_frozen); } else if(starts_with(next, MAP_TYPE)) { TypeParamsVec params; if (!parser.get_type_params(¶ms) || params.size() < 2) { return DataType::ConstPtr(); } DataType::ConstPtr key_type(parse_one(params[0], native_types)); DataType::ConstPtr value_type(parse_one(params[1], native_types)); if (!key_type || !value_type) { return DataType::ConstPtr(); } return CollectionType::map(key_type, value_type, is_frozen); } if (is_frozen) { LOG_WARN("Got a frozen type for something other than a collection, " "this driver might be too old for your version of Cassandra"); } if (is_user_type(next)) { parser.skip(); // Skip '(' std::string keyspace; if (!parser.read_one(&keyspace)) { return DataType::ConstPtr(); } parser.skip_blank_and_comma(); std::string hex; if (!parser.read_one(&hex)) { return DataType::ConstPtr(); } std::string type_name; if (!from_hex(hex, &type_name)) { LOG_ERROR("Invalid hex string \"%s\" for parameter", hex.c_str()); return DataType::ConstPtr(); } if (keyspace.empty() || type_name.empty()) { LOG_ERROR("UDT has no keyspace or type name"); return DataType::ConstPtr(); } parser.skip_blank_and_comma(); NameAndTypeParamsVec raw_fields; if (!parser.get_name_and_type_params(&raw_fields)) { return DataType::ConstPtr(); } UserType::FieldVec fields; for (NameAndTypeParamsVec::const_iterator i = raw_fields.begin(), end = raw_fields.end(); i != end; ++i) { DataType::ConstPtr data_type = parse_one(i->second, native_types); if (!data_type) { return DataType::ConstPtr(); } fields.push_back(UserType::Field(i->first, data_type)); } return DataType::ConstPtr(new UserType(keyspace, type_name, fields, true)); } if (is_tuple_type(type)) { TypeParamsVec raw_types; if (!parser.get_type_params(&raw_types)) { return DataType::ConstPtr(); } DataType::Vec types; for (TypeParamsVec::const_iterator i = raw_types.begin(), end = raw_types.end(); i != end; ++i) { DataType::ConstPtr data_type = parse_one(*i, native_types); if (!data_type) { return DataType::ConstPtr(); } types.push_back(data_type); } return DataType::ConstPtr(new TupleType(types, true)); } DataType::ConstPtr native_type(native_types.by_class_name(next)); if (native_type) { return native_type; } return DataType::ConstPtr(new CustomType(next)); }
static struct AstNode *parse_func_def( struct DomNode *dom, struct ParserState *state) { struct AstNode *expr = NULL; struct DomNode *child = NULL; struct DomNode *arg_child = NULL; struct AstNode *formal_args = NULL, *formal_args_end = NULL; /* 1. Is compound CORE. */ if (!dom_node_is_spec_compound(dom, DOM_CPD_CORE)) { return NULL; } /* 2. Has 3 children. */ if (!dom_node_is_cpd_of_size(dom, 3)) { return NULL; } child = dom->cpd_children; /* 2.1. 1st child is "func" keyword. */ if (!dom_node_is_spec_reserved_atom(child, DOM_RES_FUNC)) { return NULL; } child = child->next; /* 2.2. 2nd keyword is a core compound of patterns. */ if (!dom_node_is_spec_compound(child, DOM_CPD_CORE)) { return NULL; } arg_child = child->cpd_children; /* Argument list may be empty. */ if (arg_child) { while (arg_child) { struct AstNode *pattern; if (!(pattern = parse_one(arg_child, state))) { goto fail; } else { LIST_APPEND(pattern, &formal_args, &formal_args_end); } arg_child = arg_child->next; } } child = child->next; /* 2.3. Has 1 more further expression. */ if (!(expr = parse_one(child, state))) { goto fail; } return ast_make_spec_func_def(formal_args, expr); fail: if (formal_args) { ast_node_free(formal_args); } return NULL; }
Atom* parse_one( SourceManager::Input& in ) { while( 1 ) { switch( in.peek() ) { case '\0': return nullptr; case ' ': case '\r': case '\n': case '\t': in.eatwhite(); break; case ';': while( int c = in.next() ) { if( c == '\n' ) { break; } } break; case '(': { std::vector<Atom*> c; auto start = in.tell(); in.next(); while( Atom* a = parse_one( in ) ) { c.push_back( a ); } if( in.next() != ')' ) { Error.at( in.location(start, in.tell()) ).fmt("Missing ')' for list begun here"); throw 0; } List* l = new List(in.location(start, in.tell())); l->items.swap( c ); return l; } case ')': return nullptr; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { auto start = in.tell(); const char* s = in.peekbuf(); bool isFloat = false; while( int c = in.peek() ) { //if(isdigit(c) || isalpha(c) || c == '_'){ if( isdigit( c ) ) { in.next(); } else if( c == '.' && isFloat == false ) { isFloat = true; in.next(); } else break; } return new Number( in.location(start, in.tell() ) ); } case '"': { in.next(); auto start = in.tell(); const char* s = in.peekbuf(); while( 1 ) { switch( int c = in.next() ) { case -1: case 0: Error.at( in.location() ).fmt("End of input while parsing quoted string" ); return nullptr; case '"': { return new String( in.location(start, in.tell()-1) ); } default: break; } } break; } default: { if( isalpha( in.peek() ) || in.peek() == '_' || in.peek() == '@' ) { auto start = in.tell(); const char* s = in.peekbuf(); in.next(); while( int c = in.peek() ) { if( isdigit( c ) || isalpha( c ) || c == '@' || c == '_' || c == '?' || c == '!' ) { in.next(); } else break; } return new Symbol( in.location(start, in.tell() ) ); } else throw 0; } } } }
static struct AstNode *parse_match( struct DomNode *dom, struct ParserState *state) { struct DomNode *child = NULL; struct AstNode *expr = NULL; struct AstNode *keys = NULL, *keys_end = NULL; struct AstNode *values = NULL, *values_end = NULL; /* 1. Is compound CORE. */ if (!dom_node_is_spec_compound(dom, DOM_CPD_CORE)) { return NULL; } /* 2. Has 3 or more children. */ if (!dom_node_is_cpd_min_size(dom, 3)) { return NULL; } child = dom->cpd_children; /* 3.1. 1st child is "match" keyword. */ if (!dom_node_is_spec_reserved_atom(child, DOM_RES_MATCH)) { return NULL; } child = child->next; /* 3.2. 2nd child is an expression. */ if (!(expr = parse_one(child, state))) { return NULL; } child = child->next; /* 3.3. Has at least one matching expression. */ while (child) { struct DomNode *match_child = NULL; struct AstNode *key = NULL; struct AstNode *value = NULL; /* 3.3.1. Is compound CORE. */ if (!dom_node_is_spec_compound(child, DOM_CPD_CORE)) { goto fail; } /* 3.3.2. Has 2 children. */ if (!dom_node_is_cpd_of_size(child, 2)) { goto fail; } match_child = child->cpd_children; /* 3.3.3. 1st child is a pattern. */ if (!(key = parse_one(match_child, state))) { goto fail; } match_child = match_child->next; /* 3.3.4. 2nd child is an expression. */ if (!(value = parse_one(match_child, state))) { goto fail; } LIST_APPEND(key, &keys, &keys_end); LIST_APPEND(value, &values, &values_end); child = child->next; } return ast_make_spec_match(expr, keys, values); fail: ast_node_free(expr); if (keys) { ast_node_free(keys); } if (values) { ast_node_free(values); } return NULL; }
int parser_context::read_token(bool strict, int *sym, const char **symstart, bool eat) { int res, readnum; int pointer = 0, prevpointer = 0; int linenumber = m_current_line; int state = 0; const char *currline = m_input_line_start; int commentlevel = 0; while (1) { if ((res = parse_one(m_input_pointer + pointer, state, sym, &readnum)) < 1) { return res; } if (strict) { break; } if (*sym == _OCOMMENT) { int commentlen = readnum; pointer += readnum; commentlevel++; state = 1; while (commentlevel > 0) { if ((res = parse_one(m_input_pointer + pointer, state, sym, &readnum)) < 1) return res; if (*sym == _NEWLINE) { linenumber++; currline = m_input_pointer + pointer + readnum; } else if (*sym == _OCOMMENT) { commentlevel++; } else if (*sym == _CCOMMENT) { commentlevel--; } pointer += readnum; } pointer -= commentlen; state = 0; } else if (*sym == _LCOMMENT) { while (1) { if ((res = parse_one(m_input_pointer + pointer, 1, sym, &readnum)) < 1) return res; if (*sym == _NEWLINE) { linenumber++; currline = m_input_pointer + pointer + readnum; break; } pointer += readnum; } } else if (*sym == _NEWLINE) { linenumber++; currline = m_input_pointer + pointer + readnum; } else if (*sym != -1) { break; } prevpointer = pointer; pointer += readnum; } if (symstart) *symstart = m_input_pointer + pointer; if (eat) { m_current_line = linenumber; m_input_pointer += pointer + readnum; m_input_line_start = currline; } return 1; }
void parse_list(Reader* r, int* i, dt::StrView* result, T rule, RULES... rules) { parse_one(r, i, result, rule); parse_list(r, i, result, rules...); }
void parse_list(Reader *r, int *i, StrView *result, T rule) { parse_one(r, i, result, rule); }