Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Add a 'region' lens to make controlling spacing easier #244

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lenses/tests/test_inifile.aug
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,19 @@ ticket_243 = \"value1;value2#value3\" # end of line comment
{ "#comment" = "comment with colon" }
{ }
}

test IniFile.lns_loose_multiline get multiline_test =
{ "section" = ".anon" { "test_ace" = "val1\n val2\n val3" } }


let simpler_lens = (IniFile.empty | IniFile.comment IniFile.comment_re IniFile.comment_default)*

test simpler_lens put "## Default value: /etc/zypp
##
# configdir = /etc/zypp

##\n" after rm "/#comment[2]" =
"## Default value: /etc/zypp
# configdir = /etc/zypp

##\n"
25 changes: 25 additions & 0 deletions src/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ struct dict *make_dict(char *key, struct skel *skel, struct dict *subdict) {
return NULL;
}

static char *size_as_key(size_t key) {
char *s = NULL;

xasprintf(&s, "%zx", key);
return s;
}

struct dict *make_dictz(size_t key, struct skel *skel, struct dict *subdict) {
char *s = size_as_key(key);

if (s == NULL)
return NULL;
return make_dict(s, skel, subdict);
}

void free_dict(struct dict *dict) {
if (dict == NULL)
return;
Expand Down Expand Up @@ -213,7 +228,17 @@ void dict_lookup(const char *key, struct dict *dict,
}
}

void dict_lookupz(size_t key, struct dict *dict,
struct skel **skel, struct dict **subdict) {
char *s = size_as_key(key);

if (s == NULL) {
*skel = NULL;
*subdict = NULL;
return;
}
dict_lookup(s, dict, skel, subdict);
}

/*
* Local variables:
Expand Down
29 changes: 23 additions & 6 deletions src/get.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,9 @@ static void print_skel(struct skel *skel) {
case L_SUBTREE:
print_skel_list(skel->skels, "[", " ", "]");
break;
case L_REGION:
print_skel_list(skel->skels, "<", " ", ">");
break;
default:
printf("??");
break;
Expand Down Expand Up @@ -779,6 +782,7 @@ static int try_match(struct lens *lens, struct state *state,
return 0;
break;
case L_SUBTREE:
case L_REGION:
case L_STAR:
case L_MAYBE:
case L_SQUARE:
Expand Down Expand Up @@ -910,6 +914,7 @@ static struct tree *get_subtree(struct lens *lens, struct state *state) {
tree = make_tree(state->key, state->value, NULL, children);
ERR_NOMEM(tree == NULL, state->info);
tree->span = move(state->span);
tree->pos = REG_START(state) + 1;

if (tree->span != NULL) {
update_span(span, tree->span->span_start, tree->span->span_end);
Expand All @@ -933,7 +938,11 @@ static struct skel *parse_subtree(struct lens *lens, struct state *state,

state->key = NULL;
skel = parse_lens(lens->child, state, &di);
*dict = make_dict(state->key, skel, di);
if (getenv("AUGEAS_NO_SHIFT") == NULL) {
*dict = make_dict(state->key, skel, di);
} else {
*dict = make_dictz(REG_START(state) + 1, skel, di);
}
state->key = key;
return make_skel(lens);
}
Expand Down Expand Up @@ -1271,14 +1280,19 @@ static void parse_combine(struct rec_state *rec_state,

static void visit_exit_put_subtree(struct lens *lens,
struct rec_state *rec_state,
struct frame *top) {
struct frame *top,
size_t start) {
struct state *state = rec_state->state;
struct skel *skel = NULL;
struct dict *dict = NULL;

skel = make_skel(lens);
ERR_NOMEM(skel == NULL, lens->info);
dict = make_dict(top->key, top->skel, top->dict);
if (getenv("AUGEAS_NO_SHIFT") == NULL) {
dict = make_dict(top->key, top->skel, top->dict);
} else {
dict = make_dictz(start, top->skel, top->dict);
}
ERR_NOMEM(dict == NULL, lens->info);

top = pop_frame(rec_state);
Expand All @@ -1295,7 +1309,7 @@ static void visit_exit_put_subtree(struct lens *lens,
}

static void visit_exit(struct lens *lens,
ATTRIBUTE_UNUSED size_t start,
size_t start,
ATTRIBUTE_UNUSED size_t end,
void *data) {
struct rec_state *rec_state = data;
Expand All @@ -1311,14 +1325,15 @@ static void visit_exit(struct lens *lens,

ERR_BAIL(lens->info);

if (lens->tag == L_SUBTREE) {
if (lens->tag == L_SUBTREE || lens->tag == L_REGION) {
/* Get the result of parsing lens->child */
struct frame *top = pop_frame(rec_state);
ERR_BAIL(state->info);
if (rec_state->mode == M_GET) {
tree = make_tree(top->key, top->value, NULL, top->tree);
ERR_NOMEM(tree == NULL, lens->info);
tree->span = state->span;
tree->pos = start;
/* Restore the parse state from before entering this subtree */
top = pop_frame(rec_state);
ERR_BAIL(state->info);
Expand All @@ -1331,7 +1346,7 @@ static void visit_exit(struct lens *lens,
ERR_BAIL(state->info);
top->tree = move(tree);
} else {
visit_exit_put_subtree(lens, rec_state, top);
visit_exit_put_subtree(lens, rec_state, top, start);
}
} else if (lens->tag == L_CONCAT) {
ensure(rec_state->fused >= lens->nchildren, state->info);
Expand Down Expand Up @@ -1552,6 +1567,7 @@ static struct tree *get_lens(struct lens *lens, struct state *state) {
tree = get_union(lens, state);
break;
case L_SUBTREE:
case L_REGION:
tree = get_subtree(lens, state);
break;
case L_STAR:
Expand Down Expand Up @@ -1693,6 +1709,7 @@ static struct skel *parse_lens(struct lens *lens, struct state *state,
skel = parse_union(lens, state, dict);
break;
case L_SUBTREE:
case L_REGION:
skel = parse_subtree(lens, state, dict);
break;
case L_STAR:
Expand Down
1 change: 1 addition & 0 deletions src/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ struct tree {
char *label; /* Last component of PATH */
struct tree *children; /* List of children through NEXT */
char *value;
size_t pos;
struct span *span;

/* Flags */
Expand Down
4 changes: 4 additions & 0 deletions src/jmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,7 @@ build_nullable(struct jmt_parse *parse, ind_t pos,
lens->children[i], lvl+1);
break;
case L_SUBTREE:
case L_REGION:
case L_SQUARE:
build_nullable(parse, pos, visitor, lens->child, lvl+1);
break;
Expand Down Expand Up @@ -1217,6 +1218,7 @@ static void print_grammar(struct jmt *jmt, struct lens *lens) {
print_grammar(jmt, lens->children[i]);
break;
case L_SUBTREE:
case L_REGION:
print_lens_symbol(stdout, jmt, lens->child);
printf("\n");
print_grammar(jmt, lens->child);
Expand Down Expand Up @@ -1280,6 +1282,7 @@ static void index_lenses(struct jmt *jmt, struct lens *lens) {
index_lenses(jmt, lens->children[i]);
break;
case L_SUBTREE:
case L_REGION:
case L_STAR:
case L_MAYBE:
case L_SQUARE:
Expand Down Expand Up @@ -1475,6 +1478,7 @@ static void conv_rhs(struct jmt *jmt, ind_t l) {
conv_union(jmt, lens, &s, &e, &f);
break;
case L_SUBTREE:
case L_REGION:
conv(jmt, lens->child, &s, &e, &f);
break;
case L_STAR:
Expand Down
33 changes: 31 additions & 2 deletions src/lens.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,27 @@ struct value *lns_make_subtree(struct info *info, struct lens *l) {
return make_lens_value(lens);
}

/*
* A region lens l1 = < l >
*
* Types are simply copied
*/
struct value *lns_make_region(struct info *info, struct lens *l) {
struct lens *lens;

lens = make_lens_unop(L_REGION, info, l);
for (int t = 0; t < ntypes; t++) {
ltype(lens, t) = ref(ltype(l, t));
}
lens->value = l->value;
lens->key = l->key;
lens->recursive = l->recursive;
lens->rec_internal = l->rec_internal;
if (! l->recursive)
lens->ctype_nullable = l->ctype_nullable;
return make_lens_value(lens);
}

struct value *lns_make_star(struct info *info, struct lens *l, int check) {
struct lens *lens;

Expand Down Expand Up @@ -1016,6 +1037,7 @@ void free_lens(struct lens *lens) {
unref(lens->string, string);
break;
case L_SUBTREE:
case L_REGION:
case L_STAR:
case L_MAYBE:
case L_SQUARE:
Expand Down Expand Up @@ -1057,8 +1079,9 @@ void lens_release(struct lens *lens) {
if (lens->tag == L_KEY || lens->tag == L_STORE)
regexp_release(lens->regexp);

if (lens->tag == L_SUBTREE || lens->tag == L_STAR
|| lens->tag == L_MAYBE || lens->tag == L_SQUARE) {
if (lens->tag == L_SUBTREE || lens->tag == L_REGION
|| lens->tag == L_STAR || lens->tag == L_MAYBE
|| lens->tag == L_SQUARE) {
lens_release(lens->child);
}

Expand Down Expand Up @@ -1362,6 +1385,7 @@ static int format_atype(struct lens *l, char **buf, uint indent) {
return (*buf == NULL) ? -1 : 0;
break;
case L_SUBTREE:
case L_REGION:
return format_subtree_atype(l, buf, indent);
break;
case L_STAR:
Expand Down Expand Up @@ -1741,6 +1765,7 @@ static void rtn_rules(struct rtn *rtn, struct lens *l) {
rtn_rules(rtn, l->body);
RTN_BAIL(rtn);
break;
case L_REGION:
case L_SQUARE:
add_trans(rtn, start, prod->end, l->child);
RTN_BAIL(rtn);
Expand Down Expand Up @@ -2078,6 +2103,7 @@ static void propagate_type(struct lens *l, enum lens_type lt) {
case L_REC:
/* Nothing to do */
break;
case L_REGION:
case L_SQUARE:
propagate_type(l->child, lt);
ltype(l, lt) = ref(ltype(l->child, lt));
Expand Down Expand Up @@ -2158,6 +2184,7 @@ static struct value *typecheck(struct lens *l, int check) {
exn = typecheck_n(l, lns_make_union, check);
break;
case L_SUBTREE:
case L_REGION:
case L_SQUARE:
exn = typecheck(l->child, check);
break;
Expand Down Expand Up @@ -2273,6 +2300,7 @@ static int ctype_nullable(struct lens *lens, struct value **exn) {
}
break;
case L_SUBTREE:
case L_REGION:
case L_SQUARE:
ret = ctype_nullable(lens->child, exn);
nullable = lens->child->ctype_nullable;
Expand Down Expand Up @@ -2438,6 +2466,7 @@ void dump_lens(FILE *out, struct lens *lens){
}
break;
case L_SUBTREE:
case L_REGION:
fprintf(out, "\"%p\" -> \"%p\"\n", lens, lens->child);
dump_lens(out, lens->child);
break;
Expand Down
11 changes: 9 additions & 2 deletions src/lens.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ enum lens_tag {
L_CONCAT,
L_UNION,
L_SUBTREE,
L_REGION,
L_STAR,
L_MAYBE,
L_REC,
Expand Down Expand Up @@ -94,7 +95,8 @@ struct lens {
struct string *string; /* L_VALUE, L_LABEL, L_SEQ, L_COUNTER */
};
/* Combinators */
struct lens *child; /* L_SUBTREE, L_STAR, L_MAYBE, L_SQUARE */
struct lens *child; /* L_SUBTREE, L_STAR, L_MAYBE,
L_SQUARE, L_REGION */
struct { /* L_UNION, L_CONCAT */
unsigned int nchildren;
struct lens **children;
Expand Down Expand Up @@ -134,6 +136,7 @@ struct value *lns_make_union(struct info *, struct lens *, struct lens *,
struct value *lns_make_concat(struct info *, struct lens *, struct lens *,
int check);
struct value *lns_make_subtree(struct info *, struct lens *);
struct value *lns_make_region(struct info *, struct lens *);
struct value *lns_make_star(struct info *, struct lens *,
int check);
struct value *lns_make_plus(struct info *, struct lens *,
Expand Down Expand Up @@ -165,7 +168,7 @@ struct skel {
char *text; /* L_DEL */
struct skel *skels; /* L_CONCAT, L_STAR, L_SQUARE */
};
/* Also tag == L_SUBTREE, with no data in the union */
/* Also tag == L_SUBTREE || tag == L_REGION, with no data in the union */
};

struct lns_error {
Expand All @@ -178,8 +181,12 @@ struct lns_error {
};

struct dict *make_dict(char *key, struct skel *skel, struct dict *subdict);
struct dict *make_dictz(size_t key, struct skel *skel, struct dict *subdict);

void dict_lookup(const char *key, struct dict *dict,
struct skel **skel, struct dict **subdict);
void dict_lookupz(size_t key, struct dict *dict,
struct skel **skel, struct dict **subdict);
int dict_append(struct dict **dict, struct dict *d2);
void free_skel(struct skel *skel);
void free_dict(struct dict *dict);
Expand Down
2 changes: 1 addition & 1 deletion src/lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ ARROW ->
return REGEXP;
}

[|*?+()=:;\.\[\]{}-] return yytext[0];
[|*?+()=:;\.\[\]{}<>-] return yytext[0];

module return KW_MODULE;

Expand Down
4 changes: 3 additions & 1 deletion src/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ aexp: qid
{ $$ = $2; }
| '[' exp ']'
{ $$ = make_unop(A_BRACKET, $2, &@$); }
| '<' exp '>'
{ $$ = make_unop(A_REGION, $2, &@$); }
| '(' ')'
{ $$ = make_unit_term(&@$); }

Expand Down Expand Up @@ -524,7 +526,7 @@ static struct term *make_binop(enum term_tag tag,

static struct term *make_unop(enum term_tag tag, struct term *exp,
struct info *locp) {
assert(tag == A_BRACKET);
assert(tag == A_BRACKET || tag == A_REGION);
struct term *term = make_term_locp(tag, locp);
term->brexp = exp;
return term;
Expand Down
Loading