mirror of
https://github.com/ProgramSnail/Lama.git
synced 2025-12-06 14:58:50 +00:00
Cosmetics; probably a fix for regexps
This commit is contained in:
parent
423e4e7724
commit
9d0b8e811a
6 changed files with 72 additions and 54 deletions
|
|
@ -557,13 +557,7 @@ extern void* Lsubstring (void *subj, int p, int l) {
|
||||||
extern struct re_pattern_buffer *Lregexp (char *regexp) {
|
extern struct re_pattern_buffer *Lregexp (char *regexp) {
|
||||||
regex_t *b = (regex_t*) malloc (sizeof (regex_t));
|
regex_t *b = (regex_t*) malloc (sizeof (regex_t));
|
||||||
|
|
||||||
b->translate = 0;
|
memset (b, 0, sizeof (regex_t));
|
||||||
b->fastmap = 0;
|
|
||||||
// A weird workaround: should be 0/0 in theory,
|
|
||||||
// but is does not work sometimes. The exact number is
|
|
||||||
// determined experimentally :((
|
|
||||||
b->buffer = malloc (256);
|
|
||||||
b->allocated = 256;
|
|
||||||
|
|
||||||
int n = (int) re_compile_pattern (regexp, strlen (regexp), b);
|
int n = (int) re_compile_pattern (regexp, strlen (regexp), b);
|
||||||
|
|
||||||
|
|
@ -575,11 +569,19 @@ extern struct re_pattern_buffer *Lregexp (char *regexp) {
|
||||||
}
|
}
|
||||||
|
|
||||||
extern int LregexpMatch (struct re_pattern_buffer *b, char *s, int pos) {
|
extern int LregexpMatch (struct re_pattern_buffer *b, char *s, int pos) {
|
||||||
|
int res;
|
||||||
|
|
||||||
ASSERT_BOXED("regexpMatch:1", b);
|
ASSERT_BOXED("regexpMatch:1", b);
|
||||||
ASSERT_STRING("regexpMatch:2", s);
|
ASSERT_STRING("regexpMatch:2", s);
|
||||||
ASSERT_UNBOXED("regexpMatch:3", pos);
|
ASSERT_UNBOXED("regexpMatch:3", pos);
|
||||||
|
|
||||||
return BOX (re_match (b, s, LEN(TO_DATA(s)->tag), UNBOX(pos), 0));
|
res = re_match (b, s, LEN(TO_DATA(s)->tag), UNBOX(pos), 0);
|
||||||
|
|
||||||
|
if (res) {
|
||||||
|
return BOX (res);
|
||||||
|
}
|
||||||
|
|
||||||
|
return BOX (res);
|
||||||
}
|
}
|
||||||
|
|
||||||
extern void* Bstring (void*);
|
extern void* Bstring (void*);
|
||||||
|
|
@ -723,9 +725,6 @@ extern int Lhash (void *p) {
|
||||||
|
|
||||||
extern int Lcompare (void *p, void *q) {
|
extern int Lcompare (void *p, void *q) {
|
||||||
# define COMPARE_AND_RETURN(x,y) do if (x != y) return BOX(x - y); while (0)
|
# define COMPARE_AND_RETURN(x,y) do if (x != y) return BOX(x - y); while (0)
|
||||||
if (q == 0 || p == 0) {
|
|
||||||
failure ("NULL pointer in Lcompare\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (p == q) return BOX(0);
|
if (p == q) return BOX(0);
|
||||||
|
|
||||||
|
|
@ -735,50 +734,57 @@ extern int Lcompare (void *p, void *q) {
|
||||||
}
|
}
|
||||||
else if (UNBOXED(q)) return BOX(1);
|
else if (UNBOXED(q)) return BOX(1);
|
||||||
else {
|
else {
|
||||||
data *a = TO_DATA(p), *b = TO_DATA(q);
|
if (is_valid_heap_pointer (p)) {
|
||||||
int ta = TAG(a->tag), tb = TAG(b->tag);
|
if (is_valid_heap_pointer (q)) {
|
||||||
int la = LEN(a->tag), lb = LEN(b->tag);
|
data *a = TO_DATA(p), *b = TO_DATA(q);
|
||||||
int i;
|
int ta = TAG(a->tag), tb = TAG(b->tag);
|
||||||
|
int la = LEN(a->tag), lb = LEN(b->tag);
|
||||||
|
int i;
|
||||||
|
|
||||||
COMPARE_AND_RETURN (ta, tb);
|
COMPARE_AND_RETURN (ta, tb);
|
||||||
|
|
||||||
switch (ta) {
|
switch (ta) {
|
||||||
case STRING_TAG:
|
case STRING_TAG:
|
||||||
return BOX(strcmp (a->contents, b->contents));
|
return BOX(strcmp (a->contents, b->contents));
|
||||||
|
|
||||||
case CLOSURE_TAG:
|
case CLOSURE_TAG:
|
||||||
COMPARE_AND_RETURN (((void**) a->contents)[0], ((void**) b->contents)[0]);
|
COMPARE_AND_RETURN (((void**) a->contents)[0], ((void**) b->contents)[0]);
|
||||||
COMPARE_AND_RETURN (la, lb);
|
COMPARE_AND_RETURN (la, lb);
|
||||||
i = 1;
|
i = 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ARRAY_TAG:
|
case ARRAY_TAG:
|
||||||
COMPARE_AND_RETURN (la, lb);
|
COMPARE_AND_RETURN (la, lb);
|
||||||
i = 0;
|
i = 0;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SEXP_TAG: {
|
case SEXP_TAG: {
|
||||||
#ifndef DEBUG_PRINT
|
#ifndef DEBUG_PRINT
|
||||||
int ta = TO_SEXP(p)->tag, tb = TO_SEXP(q)->tag;
|
int ta = TO_SEXP(p)->tag, tb = TO_SEXP(q)->tag;
|
||||||
#else
|
#else
|
||||||
int ta = GET_SEXP_TAG(TO_SEXP(p)->tag), tb = GET_SEXP_TAG(TO_SEXP(q)->tag);
|
int ta = GET_SEXP_TAG(TO_SEXP(p)->tag), tb = GET_SEXP_TAG(TO_SEXP(q)->tag);
|
||||||
#endif
|
#endif
|
||||||
COMPARE_AND_RETURN (ta, tb);
|
COMPARE_AND_RETURN (ta, tb);
|
||||||
COMPARE_AND_RETURN (la, lb);
|
COMPARE_AND_RETURN (la, lb);
|
||||||
i = 0;
|
i = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
failure ("invalid tag %d in compare *****\n", ta);
|
failure ("invalid tag %d in compare *****\n", ta);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (; i<la; i++) {
|
for (; i<la; i++) {
|
||||||
int c = Lcompare (((void**) a->contents)[i], ((void**) b->contents)[i]);
|
int c = Lcompare (((void**) a->contents)[i], ((void**) b->contents)[i]);
|
||||||
if (c != BOX(0)) return BOX(c);
|
if (c != BOX(0)) return BOX(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
return BOX(0);
|
return BOX(0);
|
||||||
|
}
|
||||||
|
else return BOX(-1);
|
||||||
|
}
|
||||||
|
else if (is_valid_heap_pointer (q)) return BOX(1);
|
||||||
|
else return BOX (p - q);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -785,7 +785,7 @@ module Expr =
|
||||||
| acc -> Call (Var "alt", [s; acc])
|
| acc -> Call (Var "alt", [s; acc])
|
||||||
) ss (Var "")
|
) ss (Var "")
|
||||||
};
|
};
|
||||||
syntaxSeq[infix]: ss:syntaxBinding[infix]+ sema:(-"{" parse[infix][Val] -"}")? {
|
syntaxSeq[infix]: ss:syntaxBinding[infix]+ sema:(-"{" scope[infix][Val] -"}")? {
|
||||||
let sema, ss =
|
let sema, ss =
|
||||||
match sema with
|
match sema with
|
||||||
| Some s -> s, ss
|
| Some s -> s, ss
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
let version = "Version 1.00, 690825f54, Sat Apr 11 21:09:51 2020 +0300"
|
let version = "Version 1.00, 423e4e772, Wed Apr 15 21:53:42 2020 +0300"
|
||||||
|
|
|
||||||
|
|
@ -312,7 +312,8 @@ public fun lookupMemo (m, v) {
|
||||||
| _ ->
|
| _ ->
|
||||||
local vc = clone (v), i = case vc of #fun -> 1 | _ -> 0 esac;
|
local vc = clone (v), i = case vc of #fun -> 1 | _ -> 0 esac;
|
||||||
for skip, i < v.length, i := i + 1 do
|
for skip, i < v.length, i := i + 1 do
|
||||||
vc [i] := lookupMemo (m, vc [i])
|
local vci = lookupMemo (m, vc [i]);
|
||||||
|
vc [i] := vci
|
||||||
od;
|
od;
|
||||||
m ::= addMap (deref (m), vc, vc);
|
m ::= addMap (deref (m), vc, vc);
|
||||||
vc
|
vc
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@
|
||||||
-- reporting
|
-- reporting
|
||||||
public fun createRegexp (r, name) {
|
public fun createRegexp (r, name) {
|
||||||
local l = [regexp (r), name];
|
local l = [regexp (r), name];
|
||||||
--printf ("Created regexp %s: %x, %x\n", name, l, l[0]);
|
-- printf ("Created regexp %s: %x, %x\n", name, l, l[0]);
|
||||||
l
|
l
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -173,6 +173,15 @@ public fun observe (name, f) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public fun showStream (name) {
|
||||||
|
fun (k) {
|
||||||
|
fun (s) {
|
||||||
|
printf ("%s: %s\n", name, showMatcher (s));
|
||||||
|
k (Succ ({}, s))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun createResult () {
|
fun createResult () {
|
||||||
local errors = ref ({}),
|
local errors = ref ({}),
|
||||||
line = ref (0),
|
line = ref (0),
|
||||||
|
|
@ -182,8 +191,10 @@ fun createResult () {
|
||||||
hasValue = ref (false);
|
hasValue = ref (false);
|
||||||
|
|
||||||
fun k (x) {
|
fun k (x) {
|
||||||
|
if log then printf ("Result: %s\n", x.string) fi;
|
||||||
case x of
|
case x of
|
||||||
Succ (val, _) ->
|
Succ (val, s) ->
|
||||||
|
if log then printf ("Result stream: %s\n", showMatcher (s)) fi;
|
||||||
if deref (hasValue)
|
if deref (hasValue)
|
||||||
then failure (sprintf ("Ostap: ambiguous parsing (%s vs. %s)", deref (value).string, val.string))
|
then failure (sprintf ("Ostap: ambiguous parsing (%s vs. %s)", deref (value).string, val.string))
|
||||||
else
|
else
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue