Fixed bug in parser; perhaps we need to scrap Ostap and go back to Yacc

This commit is contained in:
Dmitry Boulytchev 2019-12-26 20:02:30 +03:00
parent 59a7d48568
commit f4467ec540
2 changed files with 37 additions and 9 deletions

View file

@ -641,15 +641,26 @@ module Expr =
primary[def][infix][atr]:
s:(s:"-"? {match s with None -> fun x -> x | _ -> fun x -> Binop ("-", Const 0, x)})
b:base[def][infix][Val] is:( "[" i:parse[def][infix][Val] "]" {`Elem i}
| -"." (%"length" {`Len} | %"string" {`Str} | f:LIDENT args:(-"(" !(Util.list)[parse def infix Val] -")")? {`Post (f, args)})
| "(" args:!(Util.list0)[parse def infix Val] ")" {`Call args}
)+
b:base[def][infix][Val] is:( "." f:LIDENT args:(-"(" !(Util.list)[parse def infix Val] -")")? {`Post (f, args)}
| "." %"length" {`Len}
| "." %"string" {`Str}
| "[" i:parse[def][infix][Val] "]" {`Elem i}
| "(" args:!(Util.list0)[parse def infix Val] ")" {`Call args}
)+
=> {match (List.hd (List.rev is)), atr with
| `Elem i, Reff -> true
| _, Reff -> false
| _, _ -> true} =>
{
{
let is =
let rec fix_is = function
| [ ] -> []
| [x] -> [x]
| `Post (f, None) :: `Call args :: tl when args != [] -> `Post (f, Some args) :: fix_is tl
| x :: tl -> x :: fix_is tl
in
fix_is is
in
let lastElem = List.hd (List.rev is) in
let is = List.rev (List.tl (List.rev is)) in
let b =

View file

@ -59,7 +59,7 @@ fun insert (m, k, v) {
esac
}
return inner (m, k, v).snd
return m.inner (k, v).snd
}
fun find (m, k) {
@ -110,15 +110,32 @@ fun validate (t) {
fi
esac
}
inner (t, fun (x) {return true})
}
local tree = {}, i;
for i := 1, i <= 100, i := i+1 do
for i := 100, i >= 1, i := i-1 do
validate (tree);
tree := insert (tree, i, i);
printf ("Inserting: %s\n", i.string);
printf ("Result : %s\n", tree.string)
od
od;
tree := {};
for i := 1, i <= 100, i := i+2 do
validate (tree);
tree := insert (tree, i, i);
printf ("Inserting: %s\n", i.string);
printf ("Result : %s\n", tree.string)
od;
for i := 2, i <= 100, i := i+2 do
validate (tree);
tree := insert (tree, i, i);
printf ("Inserting: %s\n", i.string);
printf ("Result : %s\n", tree.string)
od