Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • hammer/hammer
  • mlp/hammer
  • xentrac/hammer
  • pesco/hammer
  • letitiali/hammer
  • nobody/hammer
  • kia/hammer-sandbox
  • vyrus001/hammer
  • denleylam/hammer
9 results
Show changes
Commits on Source (37)
Showing
with 612 additions and 20 deletions
[submodule "src/bindings/haskell"]
path = src/bindings/haskell
url = git://gitorious.org/hhammer/hhammer.git
......@@ -60,12 +60,37 @@ matrix:
- compiler: clang
language: dotnet
env: BINDINGS=dotnet CC=clang
- compiler: gcc
language: java
jdk: oraclejdk7
env: BINDINGS=java M2_HOME=/tmp
- compiler: clang
language: java
jdk: oraclejdk7
env: BINDINGS=java CC=clang M2_HOME=/tmp
- compiler: gcc
language: java
jdk: openjdk7
env: BINDINGS=java M2_HOME=/tmp
- compiler: clang
language: java
jdk: openjdk7
env: BINDINGS=java CC=clang M2_HOME=/tmp
- compiler: gcc
language: java
jdk: openjdk6
env: BINDINGS=java M2_HOME=/tmp
- compiler: clang
language: java
jdk: openjdk6
env: BINDINGS=java CC=clang M2_HOME=/tmp
before_install:
- sudo apt-get update -qq
- if [ "$BINDINGS" != "none" ]; then sudo apt-get install -qq swig; fi
- if [ "$BINDINGS" == "perl" ]; then sudo add-apt-repository ppa:dns/irc -y; sudo apt-get update -qq; sudo apt-get install -qq swig=2.0.8-1irc1~12.04; fi
- if [ "$BINDINGS" == "python" ]; then sudo apt-get install -qq python-dev; fi
- if [ "$BINDINGS" == "dotnet" ]; then sudo add-apt-repository ppa:directhex/monoxide -y; sudo apt-get update -qq; sudo apt-get install -qq mono-devel mono-mcs nunit nunit-console; mozroots --import --sync; fi
- if [ "$BINDINGS" == "java" ]; then mkdir $M2_HOME/conf; cp settings.xml $M2_HOME/conf/; fi
install: true
before_script:
......
# from http://www.scons.org/wiki/JavaNativeInterface
import os
import sys
def walkDirs(path):
"""helper function to get a list of all subdirectories"""
def addDirs(pathlist, dirname, names):
"""internal function to pass to os.path.walk"""
for n in names:
f = os.path.join(dirname, n)
if os.path.isdir(f):
pathlist.append(f)
pathlist = [path]
os.path.walk(path, addDirs, pathlist)
return pathlist
def ConfigureJNI(env):
"""Configure the given environment for compiling Java Native Interface
c or c++ language files."""
if not env.get('JAVAC'):
print "The Java compiler must be installed and in the current path."
return 0
# first look for a shell variable called JAVA_HOME
java_base = os.environ.get('JAVA_HOME')
if not java_base:
if sys.platform == 'darwin':
# Apple's OS X has its own special java base directory
java_base = '/System/Library/Frameworks/JavaVM.framework'
else:
# Search for the java compiler
print "JAVA_HOME environment variable is not set. Searching for java... ",
jcdir = os.path.dirname(env.WhereIs('javac'))
if not jcdir:
print "not found."
return 0
# assuming the compiler found is in some directory like
# /usr/jdkX.X/bin/javac, java's home directory is /usr/jdkX.X
java_base = os.path.join(jcdir, "..")
print "found at " + java_base
if sys.platform == 'cygwin':
# Cygwin and Sun Java have different ideas of how path names
# are defined. Use cygpath to convert the windows path to
# a cygwin path. i.e. C:\jdkX.X to /cygdrive/c/jdkX.X
java_base = os.popen("cygpath -up '"+java_base+"'").read().replace( \
'\n', '')
if sys.platform == 'darwin':
# Apple does not use Sun's naming convention
java_headers = [os.path.join(java_base, 'Headers')]
java_libs = [os.path.join(java_base, 'Libraries')]
else:
# windows and linux
java_headers = [os.path.join(java_base, 'include')]
java_libs = [os.path.join(java_base, 'lib')]
# Sun's windows and linux JDKs keep system-specific header
# files in a sub-directory of include
if java_base == '/usr' or java_base == '/usr/local':
# too many possible subdirectories. Just use defaults
java_headers.append(os.path.join(java_headers[0], 'win32'))
java_headers.append(os.path.join(java_headers[0], 'linux'))
java_headers.append(os.path.join(java_headers[0], 'solaris'))
else:
# add all subdirs of 'include'. The system specific headers
# should be in there somewhere
java_headers = walkDirs(java_headers[0])
# add Java's include and lib directory to the environment
env.Append(CPPPATH = java_headers)
env.Append(LIBPATH = java_libs)
# add any special platform-specific compilation or linking flags
if sys.platform == 'darwin':
env.Append(SHLINKFLAGS = '-dynamiclib -framework JavaVM')
env['SHLIBSUFFIX'] = '.jnilib'
elif sys.platform == 'cygwin':
env.Append(CCFLAGS = '-mno-cygwin')
env.Append(SHLINKFLAGS = '-mno-cygwin -Wl,--kill-at')
# Add extra potentially useful environment variables
env['JAVA_HOME'] = java_base
env['JNI_CPPPATH'] = java_headers
env['JNI_LIBPATH'] = java_libs
return 1
......@@ -7,7 +7,7 @@ import sys
vars = Variables(None, ARGUMENTS)
vars.Add(PathVariable('DESTDIR', "Root directory to install in (useful for packaging scripts)", None, PathVariable.PathIsDirCreate))
vars.Add(PathVariable('prefix', "Where to install in the FHS", "/usr/local", PathVariable.PathAccept))
vars.Add(ListVariable('bindings', 'Language bindings to build', 'none', ['dotnet', 'perl', 'php', 'python']))
vars.Add(ListVariable('bindings', 'Language bindings to build', 'none', ['dotnet', 'java', 'perl', 'php', 'python']))
env = Environment(ENV = {'PATH' : os.environ['PATH']},
variables = vars,
......@@ -17,6 +17,13 @@ env = Environment(ENV = {'PATH' : os.environ['PATH']},
if not 'bindings' in env:
env['bindings'] = []
# FIXME this bit doesn't work unless JAVA_HOME is set
if 'java' in env['bindings']:
from ConfigureJNI import ConfigureJNI
if not ConfigureJNI(env):
print "Java Native Interface is required for Java bindings. Exiting."
Exit(0)
def calcInstallPath(*elements):
path = os.path.abspath(os.path.join(*map(env.subst, elements)))
if 'DESTDIR' in env:
......
......@@ -19,7 +19,7 @@
token {
parser token("95\xa2");
test "95\xa2" --> "95\xa2";
test "95\xa2" --> fail;
test "95\xa3" --> fail;
}
ch {
......@@ -87,7 +87,7 @@ uint8 {
}
int_range {
parser int_range(uint8(), 0x3, 0x10);
parser int_range(uint8(), 0x3, 0xa);
test <05> --> u0x05;
test <0b> --> fail;
}
......@@ -299,17 +299,17 @@ rightrec {
test "aa" --> ['a',['a']];
test "aaa" --> ['a',['a',['a']]];
}
ambiguous {
subparser $d = ch('d');
subparser $p = ch('+');
subparser $e = choice(sequence($e, $p, $e), $d);
# TODO: implement action/h_act_flatten
parser $e;
test "d" --> 'd';
test "d+d" --> ['d','+','d'];
test "d+d+d" --> [['d','+','d'],'+','d'];
}
## Only for GLR
#ambiguous {
# subparser $d = ch('d');
# subparser $p = ch('+');
# subparser $e = choice(sequence($e, $p, $e), $d);
# # TODO: implement action/h_act_flatten
# parser $e;
#
# test "d" --> 'd';
# test "d+d" --> ['d','+','d'];
# test "d+d+d" --> [['d','+','d'],'+','d'];
#}
% -*- prolog -*-
% Run with:
% $ swipl -q -t halt -g tsgenjava:prolog tsgenjava.pl >output-file
% Note: this needs to be run from the lib/ directory.
% So,
% swipl -q -t halt -g tsgenjava:prolog tsgenjava.pl >../src/bindings/java/src/test/java/hammer_tests.java
:- module(tsgenjava,
[gen_ts/2]).
:- expects_dialect(swi).
:- use_module(tsparser).
% TODO: build a Box-like pretty-printer
% ch_range should become chRange
underscore_to_camel([0'_, X|Xs], [Xp|Xsp]) :- !,
code_type(Xp, to_upper(X)),
underscore_to_camel(Xs,Xsp).
underscore_to_camel([0'-, X|Xs], [Xp|Xsp]) :- !,
code_type(Xp, to_upper(X)),
underscore_to_camel(Xs,Xsp).
underscore_to_camel([X|Xs],[X|Xsp]) :- !,
underscore_to_camel(Xs,Xsp).
underscore_to_camel([],[]) :- !.
format_parser_name(Name, Result) :-
atom_codes(Name, CName),
( member(0'_, CName) ->
underscore_to_camel(CName, Result0);
CName = Result0),
append("Hammer.", Result0, Result), !.
format_test_name(Name, Result) :-
atom_codes(Name, NameCodes),
underscore_to_camel(NameCodes, [CInit|CName]),
code_type(RInit, to_upper(CInit)),
append("Test", [RInit|CName], Result), !.
indent(0) --> "", !.
indent(N) -->
{N > 0},
" ",
{Np is N - 1},
indent(Np).
pp_char_guts(0x22) -->
"\\\"", !.
pp_char_guts(0x27) -->
"\\'", !.
pp_char_guts(A) -->
{ A >= 0x20, A < 0x7F } ->
[A];
"\\u00",
{ H is A >> 4, L is A /\ 0xF,
code_type(Hc, xdigit(H)),
code_type(Lc, xdigit(L)) },
[Hc,Lc].
pp_hexnum_guts(0) --> !.
pp_hexnum_guts(A) -->
{ L is A /\ 0xF,
H is A >> 4,
code_type(Lc, xdigit(L)) },
pp_hexnum_guts(H),
[Lc], !.
pp_string_guts([]) --> !.
pp_string_guts([X|Xs]) -->
pp_char_guts(X),
pp_string_guts(Xs), !.
pp_parser_args([]) --> !.
pp_parser_args([X|Rest]) -->
pp_parser(X),
pp_parser_args_rest(Rest).
pp_parser_args_rest([]) --> !.
pp_parser_args_rest([X|Xs]) -->
", ",
pp_parser(X),
pp_parser_args_rest(Xs).
pp_parser(parser(Name, Args)) -->
!,
{format_parser_name(Name,Fname)},
Fname,
"(",
pp_parser_args(Args),
")".
pp_parser(string(Str)) --> !,
"\"",
pp_string_guts(Str),
"\"", !.
pp_parser(num(0)) --> "0", !.
pp_parser(num(Num)) --> !,
( {Num < 0} ->
"-0x", {RNum is -Num}; "0x", {RNum = Num} ),
pp_hexnum_guts(RNum).
pp_parser(char(C)) --> !,
"\"", pp_char_guts(C), "\"", !.
pp_parser(ref(Name)) -->
{atom_codes(Name,CName)},
"sp_", CName, !.
pp_parser(A) -->
{ writef("WTF is a %w?\n", [A]),
!, fail
}.
pp_test_elem(decl, parser(_)) --> !.
pp_test_elem(init, parser(_)) --> !.
pp_test_elem(exec, parser(P)) -->
!, indent(2),
"parser = ",
pp_parser(P),
";\n".
pp_test_elem(decl, subparser(Name,_)) -->
!, indent(2),
"Parser ", pp_parser(ref(Name)),
" = Hammer.indirect();\n".
pp_test_elem(init, subparser(Name, Parser)) -->
!, indent(2),
pp_parser(ref(Name)), ".bindIndirect(",
pp_parser(Parser),
");\n".
pp_test_elem(exec, subparser(_,_)) --> !.
pp_test_elem(decl, test(_,_)) --> !.
pp_test_elem(init, test(_,_)) --> !.
pp_test_elem(decl, testFail(_)) --> !.
pp_test_elem(init, testFail(_)) --> !.
pp_test_elem(exec, test(Str, Result)) -->
!, indent(3),
"checkParseOK(parser, ", pp_parser(string(Str)),
", ",
pp_parse_result(Result),
");\n".
pp_test_elem(exec, testFail(Str)) -->
!, indent(3),
"checkParseFail(parser, ", pp_parser(string(Str)),
");\n".
% pp_test_elem(_, _) --> !.
pp_result_seq([]) --> !.
pp_result_seq([X|Xs]) --> !,
pp_parse_result(X),
pp_result_seq_r(Xs).
pp_result_seq_r([]) --> !.
pp_result_seq_r([X|Xs]) --> !,
", ",
pp_parse_result(X),
pp_result_seq_r(Xs).
pp_byte_seq([]) --> !.
pp_byte_seq([X|Xs]) --> !,
"(byte)",
pp_parser(num(X)),
pp_byte_seq_r(Xs).
pp_byte_seq_r([]) --> !.
pp_byte_seq_r([X|Xs]) --> !,
", (byte)",
pp_parser(num(X)),
pp_byte_seq_r(Xs).
pp_parser_bigint_str(num(Num)) --> !,
( {Num < 0} ->
"-", {RNum is -Num}; "", {RNum = Num} ),
pp_hexnum_guts(RNum).
pp_parse_result(char(C)) --> !,
"new BigInteger(\"",
pp_parser_bigint_str(num(C)),
"\", 16)".
%pp_parser(char(C)),
%".getBytes()[0]".
pp_parse_result(seq(Args)) --> !,
"new Object[]{ ", pp_result_seq(Args), "}".
pp_parse_result(none) --> !,
"null".
pp_parse_result(uint(V)) --> !,
"new BigInteger(\"", pp_parser_bigint_str(num(V)), "\", 16)".
pp_parse_result(sint(V)) --> !,
"new BigInteger(\"", pp_parser_bigint_str(num(V)), "\", 16)".
pp_parse_result(string(A)) --> !,
%"new byte[]{ ", pp_byte_seq(A), " }".
"\"", pp_string_guts(A), "\"".
%pp_parse_result(A) -->
% "\x1b[1;31m",
% {with_output_to(codes(C), write(A))},
% C,
% "\x1b[0m".
pp_test_elems(_, []) --> !.
pp_test_elems(Phase, [X|Xs]) -->
!,
pp_test_elem(Phase,X),
pp_test_elems(Phase,Xs).
pp_test_case(testcase(Name, Elems)) -->
!,
indent(1), "@Test\n",
{ format_test_name(Name, TName) },
indent(1), "public void ", TName, "() {\n",
indent(2), "Parser parser;\n",
pp_test_elems(decl, Elems),
pp_test_elems(init, Elems),
pp_test_elems(exec, Elems),
indent(1), "}\n".
pp_test_cases([]) --> !.
pp_test_cases([A|As]) -->
pp_test_case(A),
pp_test_cases(As).
pp_test_suite(Suite) -->
"package com.upstandinghackers.hammer; \n\n",
"import java.math.BigInteger;\n",
"import java.util.Arrays;\n",
"import org.testng.annotations.*;\n",
"import org.testng.Assert;\n\n",
"public class HammerTest extends TestSupport {\n\n",
indent(1), "static {\n",
indent(2), "System.loadLibrary(\"hammer-java\");\n",
indent(1), "}\n\n",
pp_test_cases(Suite),
"}\n".
gen_ts(Foo,Str) :-
phrase(pp_test_suite(Foo),Str).
prolog :-
read_tc(A),
gen_ts(A, Res),
writef("%s", [Res]).
File moved
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.upstandinghackers</groupId>
<artifactId>hammer-parent</artifactId>
<version>0.9</version>
<name>Hammer JNI Bindings Parent</name>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.1.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<modules>
<module>src/bindings/java</module>
</modules>
</project>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>/tmp/.m2/repository</localRepository>
</settings>
haskell @ 332e242d
Subproject commit 332e242d6314d1a5b8aee7bde47dd4c080f9b4e6
java/target
native/linux/target
File moved
File moved
File moved
# -*- python -*-
import os, sys
Import('env libhammer_shared testruns targets')
javaenv = env.Clone()
javaenv['ENV']['LD_LIBRARY_PATH'] = os.path.dirname(str(libhammer_shared[0]))
java_base = os.environ.get('JAVA_HOME')
if javaenv['PLATFORM'] == 'darwin':
java_headers = [os.path.join(java_base, 'Headers')]
java_libs = [os.path.join(java_base, 'Libraries')]
else:
java_headers = [os.path.join(java_base, 'include')]
java_libs = [os.path.join(java_base, 'lib')]
java_bin = os.path.join(java_base, 'bin')
javaenv.Append(CPPPATH = java_headers)
javaenv.Append(LIBPATH = java_libs)
javaenv.AppendENVPath('PATH', java_bin)
javaenv['ENV']['JAVA_HOME'] = java_base
javaenv['ENV']['JNI_CPPPATH'] = java_headers
javaenv['ENV']['JNI_LIBPATH'] = java_libs
if sys.platform.startswith('linux'):
platform = 'linux'
else:
platform = sys.platform
pom = ['pom.xml']
hammer_jar = javaenv.Command(['target/hammer-0.9.jar'], [pom, Dir('src/')], "mvn package -P " + platform)
Default(hammer_jar)
javatest = Alias("testjava", [hammer_jar], hammer_jar)
AlwaysBuild(hammer_jar)
testruns.append(javatest)
javainstallexec = javaenv.Command(None, hammer_jar, "mvn install -P " + platform)
javainstall = Alias("installjava", [javainstallexec], javainstallexec)
targets.append(javainstall)
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.upstandinghackers</groupId>
<artifactId>hammer</artifactId>
<version>0.9</version>
</parent>
<groupId>com.upstandinghackers</groupId>
<artifactId>hammer-java</artifactId>
<version>0.9</version>
<name>Hammer JNI Bindings - Java</name>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
......@@ -33,31 +33,69 @@ public class Hammer
}
}
private static byte[] toRawBytes(String input) {
byte[] bytes = new byte[input.length()];
for (int i = 0; i < input.length(); i++)
bytes[i] = (byte)input.charAt(i);
return bytes;
}
public static native ParseResult parse(Parser parser, byte[] input, int length);
public static ParseResult parse(Parser parser, String input) {
return parse(parser, Hammer.toRawBytes(input), input.length());
}
public static native Parser token(byte[] str, int length);
public static Parser token(String str) {
return token(Hammer.toRawBytes(str), str.length());
}
public static native Parser ch(byte c);
public static Parser ch(String s) {
return ch((byte)s.charAt(0));
}
public static Parser ch(int c) {
return ch((byte)c);
}
public static Parser ch(char c) {
return ch((byte)c);
}
public static native Parser chRange(byte from, byte to);
public static Parser chRange(String from, String to) {
return chRange(from.charAt(0), to.charAt(0));
}
public static Parser chRange(char from, char to) {
return chRange((byte)from, (byte)to);
}
public static Parser chRange(int from, int to) {
return chRange((byte)from, (byte)to);
}
public static native Parser intRange(Parser p, long lower, long upper);
public static native Parser bits(int len, boolean sign);
public static native Parser int64();
public static native Parser int32();
public static native Parser int16();
public static native Parser int8();
public static native Parser uInt64();
public static native Parser uInt32();
public static native Parser uInt16();
public static native Parser uInt8();
public static native Parser uint64();
public static native Parser uint32();
public static native Parser uint16();
public static native Parser uint8();
public static native Parser whitespace(Parser p);
public static native Parser left(Parser p, Parser q);
public static native Parser right(Parser p, Parser q);
public static native Parser middle(Parser p, Parser x, Parser q);
// public static native Parser action(Parser p, Action a);
public static native Parser in(byte[] charset, int length);
public static Parser in(String s) {
return in(s.getBytes(), s.length());
}
public static native Parser notIn(byte[] charset, int length);
public static Parser notIn(String s) {
return notIn(s.getBytes(), s.length());
}
public static native Parser endP();
public static native Parser nothingP();
public static native Parser sequence(Parser... parsers);
public static native Parser choice(Parser... parsers);
public static native Parser butNot(Parser p1, Parser p2);
public static native Parser butnot(Parser p1, Parser p2);
public static native Parser difference(Parser p1, Parser p2);
public static native Parser xor(Parser p1, Parser p2);
public static native Parser many(Parser p);
......
package com.upstandinghackers.hammer;
import java.math.BigInteger;
public class ParsedToken
{
......@@ -14,8 +15,8 @@ public class ParsedToken
public native int getIndex();
public native byte getBitOffset();
public native byte[] getBytesValue();
public native long getSIntValue();
public native long getUIntValue();
public native BigInteger getSIntValue();
public native BigInteger getUIntValue();
public native double getDoubleValue();
public native float getFloatValue();
public native ParsedToken[] getSeqValue();
......@@ -25,8 +26,8 @@ public class ParsedToken
native void setIndex(int index);
native void setBitOffset(byte offset);
native void setBytesValue(byte[] value);
native void setSIntValue(long value);
native void setUIntValue(long value);
native void setSIntValue(long value); // TODO: Change these to take a biginteger
native void setUIntValue(long value); // TODO: Change these to take a biginteger
native void setDoubleValue(double value);
native void setFloatValue(float value);
native void setSeqValue(ParsedToken value[]);
......@@ -37,4 +38,53 @@ public class ParsedToken
private long inner;
ParsedToken(long inner) {this.inner=inner;}
private void write(StringBuilder b) {
switch (getTokenType()) {
case BYTES:
byte[] bytes = getBytesValue();
for (int i = 0; i < bytes.length; i++) {
b.append(i == 0 ? "<" : ".");
String byteStr = Integer.toHexString(((int)bytes[i] + 256)%256);
if (byteStr.length() < 2) {
b.append("0");
}
b.append(byteStr);
}
b.append(">");
break;
case SINT:
b.append("s");
b.append(getSIntValue());
break;
case UINT:
b.append("u");
b.append(getUIntValue());
break;
case SEQUENCE:
boolean first = true;
b.append("[");
for (ParsedToken tok : getSeqValue()) {
if (!first)
b.append(' ');
first = false;
if (tok == null)
// I don't think this can ever be the case
b.append("null");
else
tok.write(b);
}
b.append("]");
break;
case NONE:
b.append("NONE");
}
}
public String toString() {
StringBuilder b = new StringBuilder();
write(b);
return b.toString();
}
}