Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add a 'regex global state' object
  • Loading branch information
sorear committed Oct 15, 2010
1 parent 6d9e43e commit 9615df7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
47 changes: 31 additions & 16 deletions lib/Cursor.cs
Expand Up @@ -3,6 +3,17 @@
using System.Collections.Generic;
using System.Text;

// global stuff
public sealed class GState {
public char[] orig_a;
public string orig_s;

public GState(string orig) {
orig_s = orig;
orig_a = orig.ToCharArray();
}
}

// stuff that should 'N'est, like subrules do
public sealed class NState {
public NState next;
Expand Down Expand Up @@ -71,8 +82,8 @@ public sealed class RxFrame {
// .from in matches
public int from;

public GState global;
// our backing string, in a cheap to index form
public string orig_s;
public char[] orig;
// cache of orig.Length
public int end;
Expand All @@ -81,8 +92,8 @@ public sealed class RxFrame {
public readonly Choice rootf;

public RxFrame(string name, Cursor csr) {
orig = csr.backing_ca;
orig_s = csr.backing;
global = csr.global;
orig = global.orig_a;
end = orig.Length;
rootf = bt = csr.xact;
st.ns = new NState(rootf, "RULE " + name, csr.nstate);
Expand Down Expand Up @@ -234,7 +245,7 @@ public sealed class RxFrame {

public void LTMPushAlts(Lexer lx, int[] addrs) {
PushCutGroup("LTM");
int[] cases = lx.Run(orig_s, st.pos);
int[] cases = lx.Run(global.orig_s, st.pos);
for (int i = cases.Length - 1; i >= 0; i--) {
PushBacktrack(addrs[cases[i]]);
}
Expand Down Expand Up @@ -265,11 +276,11 @@ public sealed class RxFrame {
}

public Cursor MakeCursor() {
return new Cursor(st.ns.klass, st.ns, bt, orig_s, orig, st.pos);
return new Cursor(global, st.ns.klass, st.ns, bt, st.pos);
}

public Cursor MakeMatch() {
return new Cursor(orig_s, from, st.pos, st.captures);
return new Cursor(global, st.ns.klass, from, st.pos, st.captures);
}

public static DynMetaObject MatchMO;
Expand Down Expand Up @@ -324,33 +335,35 @@ public class Cursor : IP6 {
Environment.GetEnvironmentVariable("NIECZA_RX_TRACE") != null;

// common fields
public string backing;
public char[] backing_ca;
public GState global;
public int pos;
// Cursor only
public Choice xact;
public NState nstate;
// Match only
public int from;
public CapInfo captures;
public DynMetaObject save_klass;

public string GetBacking() { return global.orig_s; }

public Cursor(IP6 proto, string text)
: this(proto.mo, null, null, text, text.ToCharArray(), 0) { }
: this(new GState(text), proto.mo, null, null, 0) { }

public Cursor(string backing, int from, int pos, CapInfo captures) {
this.backing = backing;
public Cursor(GState g, DynMetaObject klass, int from, int pos, CapInfo captures) {
this.global = g;
this.captures = captures;
this.pos = pos;
this.from = from;
this.mo = RxFrame.MatchMO;
this.save_klass = klass;
}

public Cursor(DynMetaObject klass, NState ns, Choice xact, string backing, char[] backing_ca, int pos) {
public Cursor(GState g, DynMetaObject klass, NState ns, Choice xact, int pos) {
this.mo = klass;
this.xact = xact;
this.nstate = ns;
this.backing = backing;
this.backing_ca = backing_ca;
this.global = g;
this.pos = pos;
}

Expand All @@ -359,7 +372,7 @@ public Cursor(IP6 proto, string text)
}

public Cursor At(int npos) {
return new Cursor(mo, nstate, xact, backing, backing_ca, npos);
return new Cursor(global, mo, nstate, xact, npos);
}

// TODO: keep variables around so { $<foo> = 1 } will work
Expand Down Expand Up @@ -397,6 +410,8 @@ public Cursor(IP6 proto, string text)
}

public Variable SimpleWS() {
string backing = global.orig_s;
char[] backing_ca = global.orig_a;
int l = backing_ca.Length;
int p = pos;

Expand Down Expand Up @@ -1094,7 +1109,7 @@ public class Lexer {
kl.name, name);
}
Cursor c = (Cursor)cursor;
int[] brnum = l.Run(c.backing, c.pos);
int[] brnum = l.Run(c.global.orig_s, c.pos);
IP6[] ret = new IP6[brnum.Length];
for (int i = 0; i < brnum.Length; i++)
ret[i] = candidates[brnum[i]];
Expand Down
2 changes: 1 addition & 1 deletion src/CgOp.pm
Expand Up @@ -204,7 +204,7 @@ use warnings;
sub cursor_pos { getfield('pos', $_[0]) }
sub cursor_from { getfield('from', $_[0]) }
sub cursor_butpos { rawcall($_[0], 'At', $_[1]) }
sub cursor_backing { getfield('backing', $_[0]) }
sub cursor_backing { rawcall($_[0], 'GetBacking:m,String') }
sub cursor_dows { rawcall($_[0], 'SimpleWS') }
sub cursor_item { rawcall($_[0], 'GetKey', $_[1]) }

Expand Down

0 comments on commit 9615df7

Please sign in to comment.