Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 394ba8f8b35e
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: ddbcc8543f98
Choose a head ref
  • 3 commits
  • 4 files changed
  • 1 contributor

Commits on Dec 6, 2015

  1. Copy the full SHA
    2986e5f View commit details
  2. Copy the full SHA
    f4e4134 View commit details
  3. Copy the full SHA
    ddbcc85 View commit details
79 changes: 41 additions & 38 deletions truffle/src/main/antlr4/org/jruby/truffle/format/parser/Pack.g4
Original file line number Diff line number Diff line change
@@ -11,43 +11,46 @@ grammar Pack;

sequence : directive* ;

directive : C count? # character
| (S NATIVE? LITTLE | 'v') count? # shortLittle
| (S NATIVE? BIG | 'n') count? # shortBig
| S NATIVE? count? # shortNative
| I NATIVE? count? # intNative
| (L LITTLE | 'V') count? # longLittle
| (L BIG | 'N') count? # longBig
| L count? # longNative
| (Q | L NATIVE) LITTLE count? # quadLittle
| (Q | L NATIVE) BIG count? # quadBig
| (Q | L NATIVE) count? # quadNative
| 'U' count? # utf8Character
| 'w' count? # berInteger
| D count? # doubleNative
| F count? # floatNative
| 'E' count? # doubleLittle
| 'e' count? # floatLittle
| 'G' count? # doubleBig
| 'g' count? # floatBig
| 'A' count? # binaryStringSpacePadded
| 'a' count? # binaryStringNullPadded
| 'Z' count? # binaryStringNullStar
| 'B' count? # bitStringMSBFirst
| 'b' count? # bitStringMSBLast
| 'H' count? # hexStringHighFirst
| 'h' count? # hexStringLowFirst
| 'u' count? # uuString
| 'M' INT? # mimeString
| 'm' count? # base64String
| ('p' | 'P') # pointer
| '@' INT? # at
| 'X' count? # back
| 'x' count? # nullByte
| '(' directive+ ')' INT # subSequence
| ('v' | 'n' | 'V' | 'N' | 'U' | 'w' | D | F | 'E' | 'e' | 'g' | 'G' | 'A' | 'a' | 'Z' | 'B' | 'b' | 'H'
| 'h' | 'u' | 'M' | 'm' | 'p' | 'P' | 'X' | 'x') '_'
{ notifyErrorListeners("'_' allowed only after types sSiIlLqQ"); } #errorUnderscore ;
directive : C count? # character
| (S NATIVE? LITTLE | 'v') count? # shortLittle
| (S NATIVE? BIG | 'n') count? # shortBig
| S NATIVE? count? # shortNative
| I (LITTLE NATIVE? | NATIVE? LITTLE) count? # intLittle
| I (BIG NATIVE? | NATIVE? BIG) count? # intBig
| I NATIVE? count? # intNative
| (L LITTLE | 'V') count? # longLittle
| (L BIG | 'N') count? # longBig
| L count? # longNative
| (Q | L NATIVE) LITTLE count? # quadLittle
| (Q | L NATIVE) BIG count? # quadBig
| (Q | L NATIVE) count? # quadNative
| 'U' count? # utf8Character
| 'w' count? # berInteger
| D count? # doubleNative
| F count? # floatNative
| 'E' count? # doubleLittle
| 'e' count? # floatLittle
| 'G' count? # doubleBig
| 'g' count? # floatBig
| 'A' count? # binaryStringSpacePadded
| 'a' count? # binaryStringNullPadded
| 'Z' count? # binaryStringNullStar
| 'B' count? # bitStringMSBFirst
| 'b' count? # bitStringMSBLast
| 'H' count? # hexStringHighFirst
| 'h' count? # hexStringLowFirst
| 'u' count? # uuString
| 'M' INT? # mimeString
| 'm' count? # base64String
| ('p' | 'P') # pointer
| '@' INT? # at
| 'X' count? # back
| 'x' count? # nullByte
| '(' directive+ ')' INT # subSequence
| ('v' | 'n' | 'V' | 'N' | 'U' | 'w' | D |
F | 'E' | 'e' | 'g' | 'G' | 'A' | 'a' |
'Z' | 'B' | 'b' | 'H' | 'h' | 'u' | 'M' |
'm' | 'p' | 'P' | 'X' | 'x') NATIVE #errorDisallowedNative ;

count : INT | '*' ;

@@ -61,7 +64,7 @@ F : [fF] ;

LITTLE : '<' ;
BIG : '>' ;
NATIVE : '_' | '!' ;
NATIVE : [_!] ;

INT : [0-9]+ ;

Original file line number Diff line number Diff line change
@@ -52,6 +52,8 @@ public Object write(VirtualFrame frame, Object nil) {
for (int n = 0; n < width; n++) {
writeByte(frame, padding);
}
} else if (appendNull) {
writeByte(frame, (byte) 0);
}

return null;
Original file line number Diff line number Diff line change
@@ -43,7 +43,7 @@ public CallTarget compile(String format) {

final PackParser parser = new PackParser(tokens);

final PackTreeBuilder builder = new PackTreeBuilder(context);
final PackTreeBuilder builder = new PackTreeBuilder(context, currentNode);
parser.addParseListener(builder);

parser.removeErrorListeners();
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
*/
package org.jruby.truffle.format.parser;

import com.oracle.truffle.api.nodes.Node;
import org.jruby.truffle.format.nodes.PackNode;
import org.jruby.truffle.format.nodes.SourceNode;
import org.jruby.truffle.format.nodes.control.*;
@@ -31,16 +32,19 @@

import org.jruby.truffle.format.parser.PackBaseListener;
import org.jruby.truffle.format.parser.PackParser;
import org.jruby.truffle.runtime.control.RaiseException;

public class PackTreeBuilder extends PackBaseListener {

private final RubyContext context;
private final Node currentNode;

private PackEncoding encoding = PackEncoding.DEFAULT;
private final Deque<List<PackNode>> sequenceStack = new ArrayDeque<>();

public PackTreeBuilder(RubyContext context) {
public PackTreeBuilder(RubyContext context, Node currentNode) {
this.context = context;
this.currentNode = currentNode;
pushSequence();
}

@@ -74,6 +78,16 @@ public void exitShortNative(PackParser.ShortNativeContext ctx) {
appendNode(applyCount(ctx.count(), writeInteger(16, ByteOrder.nativeOrder())));
}

@Override
public void exitIntLittle(PackParser.IntLittleContext ctx) {
appendNode(applyCount(ctx.count(), writeInteger(32, ByteOrder.LITTLE_ENDIAN)));
}

@Override
public void exitIntBig(PackParser.IntBigContext ctx) {
appendNode(applyCount(ctx.count(), writeInteger(32, ByteOrder.BIG_ENDIAN)));
}

@Override
public void exitIntNative(PackParser.IntNativeContext ctx) {
appendNode(applyCount(ctx.count(), writeInteger(32, ByteOrder.nativeOrder())));
@@ -188,7 +202,7 @@ public void exitBinaryStringNullPadded(PackParser.BinaryStringNullPaddedContext

@Override
public void exitBinaryStringNullStar(PackParser.BinaryStringNullStarContext ctx) {
binaryString((byte) 0, true, true, ctx.count());
binaryString((byte) 0, true, ctx.count() != null && ctx.count().INT() == null, ctx.count());
}

@Override
@@ -301,7 +315,9 @@ public void exitAt(PackParser.AtContext ctx) {

@Override
public void exitBack(PackParser.BackContext ctx) {
appendNode(applyCount(ctx.count(), new BackNode(context)));
if (ctx.count() == null || ctx.count().INT() != null) {
appendNode(applyCount(ctx.count(), new BackNode(context)));
}
}

@Override
@@ -319,6 +335,11 @@ public void exitSubSequence(PackParser.SubSequenceContext ctx) {
popSequence();
}

@Override
public void exitErrorDisallowedNative(PackParser.ErrorDisallowedNativeContext ctx) {
throw new RaiseException(context.getCoreLibrary().argumentError("'" + ctx.NATIVE().getText() + "' allowed only after types sSiIlLqQ", currentNode));
}

public PackNode getNode() {
return sequenceStack.peek().get(0);
}