Skip to content

Commit 831e9e2

Browse files
committed
java WIP
1 parent 4996c62 commit 831e9e2

6 files changed

Lines changed: 67 additions & 169 deletions

File tree

rapidyaml/src/main/java/org/rapidyaml/Evt.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public class Evt {
3636
// (may be fused with FLOW
3737
// if needed)
3838

39+
// Directives
40+
public static final int YAML = 1 << 22; // `%YAML <version>` followed by version string
41+
public static final int TAGD = 1 << 23; // tag directive name : `%TAG <name> .......` followed by name string
42+
public static final int TAGV = 1 << 24; // tag directive value: `%TAG ...... <value>` followed by value string
43+
3944
// Buffer flags
4045
///< IMPORTANT. Marks events whose string was placed in the
4146
///< arena. Fhis happens when the filtered string is larger than the
@@ -44,19 +49,23 @@ public class Evt {
4449
///< expand from two to three bytes). Because of this size
4550
///< expansion, the filtered string cannot be placed in the original
4651
///< source and needs to be placed in the arena.
47-
public static final int AREN = 1 << 22;
52+
public static final int AREN = 1 << 25;
4853
///< special flag to enable look back in the event array. it
4954
///< signifies that the previous event has a string, meaning that
5055
///< the jump back to that event is 3 positions. without this flag it
5156
///< would be impossible to jump to the previous event
52-
public static final int PSTR = (1 << 23);
57+
public static final int PSTR = 1 << 26;
5358
///< special flag to mark a scalar as unfiltered (when the parser
5459
///< is set not to filter)
55-
public static final int UNFILT = (1 << 24);
60+
public static final int UNFILT = 1 << 27;
5661

5762
// Utility flags/masks
58-
public static final int LAST = UNFILT;
59-
public static final int MASK = ((LAST << 1) - 1);
60-
public static final int HAS_STR = SCLR|ALIA|ANCH|TAG_;
63+
public static final int LAST = UNFILT; ///< the last flag defined above
64+
public static final int MASK = (LAST << 1) - 1; ///< a mask of all bits in this enumeration
65+
/// with string: mask of all the events that encode a string
66+
/// following the event. in the event has a string. the next two
67+
/// integers will provide respectively the string's offset and
68+
/// length. See also @ref PSTR.
69+
public static final int WSTR = SCLR|ALIA|ANCH|TAG_|TAGD|TAGV|YAML;
6170

6271
}

rapidyaml/src/main/java/org/rapidyaml/Rapidyaml.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ public boolean parseYsDirect(String filename, ByteBuffer src, ByteBuffer arena,
101101
throw new RuntimeException("arena must be direct");
102102
if (! evt.isDirect())
103103
throw new RuntimeException("evt must be direct");
104-
// the byte order for src does not matter
105-
// but for evt it really does
104+
// the byte order for src+arena does not matter
105+
// but for evt it really does:
106106
if (evt.order() != ByteOrder.nativeOrder())
107107
throw new RuntimeException("evt byte order must be native");
108108
long t = timingStart("ysparseBuf");
@@ -118,10 +118,11 @@ public boolean parseYsDirect(String filename, ByteBuffer src, ByteBuffer arena,
118118
}
119119

120120
/** Get the required size for the event output buffer, from the last parse call */
121-
public int reqsizeEvt() { return ysparse_reqsize_evt(this.ysparse); }
121+
public int requiredSizeEvt() { return ysparse_reqsize_evt(this.ysparse); }
122122
/** Get the required size for the arena buffer, from the last parse call */
123-
public int reqsizeArena() { return ysparse_reqsize_arena(this.ysparse); }
123+
public int requiredSizeArena() { return ysparse_reqsize_arena(this.ysparse); }
124124

125+
/** a helper to create a direct int buffer */
125126
public static IntBuffer mkIntBuffer(int numInts) {
126127
ByteBuffer bb = ByteBuffer.allocateDirect(/*numBytes*/4 * numInts);
127128
// !!! need to explicitly set the byte order to the native order

rapidyaml/src/main/java/org/rapidyaml/cmp/CmpEvt.java

Lines changed: 0 additions & 104 deletions
This file was deleted.

rapidyaml/src/main/java/org/rapidyaml/cmp/manifest.mf

Lines changed: 0 additions & 1 deletion
This file was deleted.

rapidyaml/src/main/java/org/rapidyaml/cmp/run.sh

Lines changed: 0 additions & 22 deletions
This file was deleted.

rapidyaml/src/test/java/org/rapidyaml/RapidyamlTest.java

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -166,18 +166,18 @@ public void testTaggedSeq()
166166
};
167167
testEvt_(ys, expected);
168168
}
169-
169+
170170
public void testDocTag()
171171
{
172172
String ys = "!yamlscript/v0/bare\n--- !code\n42\n";
173173
ExpectedEvent[] expected = {
174174
mkev(Evt.BSTR),
175175
mkev(Evt.BDOC),
176-
mkev(Evt.VAL_|Evt.TAG_, 1, 18, "yamlscript/v0/bare"),
176+
mkev(Evt.VAL_|Evt.TAG_, 0, 19, "!yamlscript/v0/bare"),
177177
mkev(Evt.VAL_|Evt.SCLR|Evt.PLAI, 0, 0, ""),
178178
mkev(Evt.EDOC),
179179
mkev(Evt.BDOC|Evt.EXPL),
180-
mkev(Evt.VAL_|Evt.TAG_, 25, 4, "code"),
180+
mkev(Evt.VAL_|Evt.TAG_, 24, 5, "!code"),
181181
mkev(Evt.VAL_|Evt.SCLR|Evt.PLAI, 30, 2, "42"),
182182
mkev(Evt.EDOC),
183183
mkev(Evt.ESTR),
@@ -207,7 +207,7 @@ public void testLargeCase()
207207
ExpectedEvent[] expected = {
208208
mkev(Evt.BSTR),
209209
mkev(Evt.BDOC|Evt.EXPL),
210-
mkev(Evt.VAL_|Evt.TAG_, 5, 13, "yamlscript/v0"),
210+
mkev(Evt.VAL_|Evt.TAG_, 4, 14, "!yamlscript/v0"),
211211
mkev(Evt.VAL_|Evt.BMAP|Evt.BLCK),
212212
mkev(Evt.KEY_|Evt.SCLR|Evt.PLAI, 19, 3, "foo"),
213213
mkev(Evt.VAL_|Evt.TAG_, 25, 0, ""),
@@ -248,7 +248,7 @@ public void testLargeCase()
248248
};
249249
testEvt_(ys, expected);
250250
}
251-
251+
252252
public void testFilterCase()
253253
{
254254
String ys = "" +
@@ -286,16 +286,17 @@ public void testFilterCase()
286286
};
287287
testEvt_(ys, expected);
288288
}
289-
289+
290290
public void testFailure() throws Exception
291291
{
292292
Rapidyaml rapidyaml = new Rapidyaml();
293293
String ys = "{a: b";
294294
byte[] src = ys.getBytes(StandardCharsets.UTF_8);
295+
byte[] arena = new byte[src.length];
295296
byte[] srcbuf = new byte[src.length];
296297
boolean gotit = false;
297298
try {
298-
callEvt(src, srcbuf);
299+
callParse(src, srcbuf, arena);
299300
}
300301
catch(YamlParseErrorException e) {
301302
gotit = true;
@@ -313,17 +314,18 @@ public void testFailure() throws Exception
313314
}
314315
assertTrue(gotit);
315316
}
316-
317+
317318
public void testFailureBuf() throws Exception
318319
{
319320
Rapidyaml rapidyaml = new Rapidyaml();
320321
String ys = "{a: b";
321322
byte[] src = ys.getBytes(StandardCharsets.UTF_8);
323+
ByteBuffer arena = ByteBuffer.allocateDirect(src.length);
322324
ByteBuffer bbuf = ByteBuffer.allocateDirect(src.length);
323325
bbuf.put(src);
324326
boolean gotit = false;
325327
try {
326-
callEvtBuf(src, bbuf);
328+
callParseDirect(src, bbuf, arena);
327329
}
328330
catch(YamlParseErrorException e) {
329331
gotit = true;
@@ -347,9 +349,10 @@ private void testEvt_(String ys, ExpectedEvent[] expected)
347349
{
348350
byte[] src = ys.getBytes(StandardCharsets.UTF_8);
349351
byte[] srcbuf = new byte[src.length];
352+
byte[] arenaarr = new byte[src.length];
350353
int[] actual;
351354
try {
352-
actual = callEvt(src, srcbuf);
355+
actual = callParse(src, srcbuf, arenaarr);
353356
}
354357
catch (Exception e) {
355358
fail("parse error:\n" + e.getMessage());
@@ -364,11 +367,12 @@ private void testEvt_(String ys, ExpectedEvent[] expected)
364367
}
365368
//------
366369
src = ys.getBytes(StandardCharsets.UTF_8);
370+
ByteBuffer arenabuf = ByteBuffer.allocateDirect(src.length);
367371
ByteBuffer bbuf = ByteBuffer.allocateDirect(src.length);
368372
bbuf.put(src);
369373
IntBuffer buf;
370374
try {
371-
buf = callEvtBuf(src, bbuf);
375+
buf = callParseDirect(src, bbuf, arenabuf);
372376
actual = buf2arr(buf);
373377
}
374378
catch (Exception e) {
@@ -470,43 +474,54 @@ public static int[] buf2arr(IntBuffer evt)
470474
return ret;
471475
}
472476

473-
static int[] callEvt(byte[] src, byte[] srcbuf) throws Exception
477+
static int[] callParse(byte[] src, byte[] srcbuf, byte[] arena) throws Exception
474478
{
475479
Rapidyaml rapidyaml = new Rapidyaml();
476480
System.arraycopy(src, 0, srcbuf, 0, src.length);
477481
int[] evt = new int[10000];
478-
int reqsize = rapidyaml.parseYsToEvt(srcbuf, evt);
479-
if(reqsize > evt.length) {
480-
evt = new int[reqsize];
482+
boolean fitsBuffers = rapidyaml.parseYs(srcbuf, arena, evt);
483+
int reqsizeInts = rapidyaml.requiredSizeEvt();
484+
int reqsizeArena = rapidyaml.requiredSizeArena();
485+
if(!fitsBuffers) {
486+
if(reqsizeInts > evt.length) evt = new int[reqsizeInts];
487+
if(reqsizeArena > arena.length) throw new RuntimeException("resize arena");
481488
System.arraycopy(src, 0, srcbuf, 0, src.length);
482-
int reqsize2 = rapidyaml.parseYsToEvt(srcbuf, evt);
483-
if(reqsize2 != reqsize) {
484-
throw new RuntimeException("reqsize");
485-
}
486-
return evt;
489+
boolean fitsBuffers2 = rapidyaml.parseYs(srcbuf, arena, evt);
490+
int reqsizeInts2 = rapidyaml.requiredSizeEvt();
491+
int reqsizeArena2 = rapidyaml.requiredSizeArena();
492+
if(fitsBuffers2 != fitsBuffers) throw new RuntimeException("fitsBuffers");
493+
if(reqsizeInts2 != reqsizeInts) throw new RuntimeException("reqsizeInts");
494+
if(reqsizeArena2 != reqsizeArena) throw new RuntimeException("reqsizeArena");
487495
}
488-
int[] ret = new int[reqsize];
489-
System.arraycopy(evt, 0, ret, 0, reqsize);
496+
int[] ret = new int[reqsizeInts];
497+
System.arraycopy(evt, 0, ret, 0, reqsizeInts);
490498
return ret;
491499
}
492500

493-
static IntBuffer callEvtBuf(byte[] src, ByteBuffer srcbuf) throws Exception
501+
static IntBuffer callParseDirect(byte[] src, ByteBuffer srcbuf, ByteBuffer arena) throws Exception
494502
{
495503
Rapidyaml rapidyaml = new Rapidyaml();
504+
arena.position(0);
496505
srcbuf.position(0);
497506
srcbuf.put(src);
498507
IntBuffer evt = Rapidyaml.mkIntBuffer(10000);
499-
int reqsize = rapidyaml.parseYsToEvtBuf(srcbuf, evt);
500-
if(reqsize > evt.capacity()) {
501-
evt = Rapidyaml.mkIntBuffer(reqsize);
508+
boolean fitsBuffers = rapidyaml.parseYsDirect(srcbuf, arena, evt);
509+
int reqsizeInts = rapidyaml.requiredSizeEvt();
510+
int reqsizeArena = rapidyaml.requiredSizeArena();
511+
if(!fitsBuffers) {
512+
if(reqsizeInts > srcbuf.position()) evt = Rapidyaml.mkIntBuffer(reqsizeInts);
513+
if(reqsizeArena > arena.capacity()) throw new RuntimeException("resize arena");
514+
arena.position(0);
502515
srcbuf.position(0);
503516
srcbuf.put(src);
504-
int reqsize2 = rapidyaml.parseYsToEvtBuf(srcbuf, evt);
505-
if(reqsize2 != reqsize) {
506-
throw new RuntimeException("reqsize");
507-
}
517+
boolean fitsBuffers2 = rapidyaml.parseYsDirect(srcbuf, arena, evt);
518+
int reqsizeInts2 = rapidyaml.requiredSizeEvt();
519+
int reqsizeArena2 = rapidyaml.requiredSizeArena();
520+
if(fitsBuffers2 != fitsBuffers) throw new RuntimeException("fitsBuffers");
521+
if(reqsizeInts2 != reqsizeInts) throw new RuntimeException("reqsizeInts");
522+
if(reqsizeArena2 != reqsizeArena) throw new RuntimeException("reqsizeArena");
508523
}
509-
evt.position(reqsize);
524+
evt.position(reqsizeInts);
510525
return evt;
511526
}
512527

@@ -545,7 +560,7 @@ class ExpectedEvent
545560
}
546561
int required_size()
547562
{
548-
return ((flags & Evt.HAS_STR) != 0) ? 3 : 1;
563+
return ((flags & Evt.WSTR) != 0) ? 3 : 1;
549564
}
550565

551566
public static int required_size_(ExpectedEvent[] evts)

0 commit comments

Comments
 (0)