Skip to content

Commit 9c2b1c1

Browse files
committed
Set up draft tree grammar and classes for Fix, analogous to Flux
See metafacture/metafacture-fix#298
1 parent 3047e2a commit 9c2b1c1

File tree

5 files changed

+183
-12
lines changed

5 files changed

+183
-12
lines changed

metafacture-fix/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 Christoph Böhme
2+
* Copyright 2024 Fabian Steeg, hbz
33
*
44
* Licensed under the Apache License, Version 2.0 the "License";
55
* you may not use this file except in compliance with the License.
@@ -14,8 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
ext.mavenName = 'Metafacture Flux'
18-
description = 'A domain specific language for building Metafacture pipelines'
17+
ext.mavenName = 'Metafacture Fix'
18+
description = 'A domain specific language for building Metafacture transformations'
1919

2020
apply plugin: 'antlr'
2121

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2024 Fabian Steeg, hbz
3+
*
4+
* Licensed under the Apache License, Version 2.0 the "License";
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
tree grammar RecordTransformerBuilder;
17+
18+
options {
19+
language = Java;
20+
tokenVocab = Fix;
21+
ASTLabelType = CommonTree;
22+
}
23+
24+
@header {
25+
package org.metafacture.fix.parser;
26+
27+
import java.util.Map;
28+
import java.util.HashMap;
29+
import java.util.ArrayList;
30+
import java.util.List;
31+
import java.util.Collections;
32+
import org.metafacture.fix.parser.RecordTransformer;
33+
}
34+
35+
@members {
36+
private RecordTransformer transformer = new RecordTransformer();
37+
}
38+
39+
transformer returns [RecordTransformer retValue = transformer] : expression*;
40+
41+
expression : (doExpr | unless | ifExpr | methodCall | COMMENT) WS*;
42+
43+
methodCall : method=ID '(' param=(ID|option) (',' (ID|option))* ')'
44+
{
45+
transformer.processMethod($method.text, $param.text);
46+
};
47+
48+
doExpr : 'do' methodCall expression* 'end';
49+
50+
unless : 'unless' methodCall expression* 'end';
51+
52+
ifExpr : 'if' methodCall expression* elsIf* elseExpr? 'end';
53+
54+
elsIf : 'elsif' methodCall expression*;
55+
56+
elseExpr: 'else' expression*;
57+
58+
option : ID ':' ID;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2024 Fabian Steeg, hbz
3+
*
4+
* Licensed under the Apache License, Version 2.0 the "License";
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.metafacture.fix;
18+
19+
import org.metafacture.fix.parser.FixLexer;
20+
import org.metafacture.fix.parser.FixParser;
21+
import org.metafacture.fix.parser.RecordTransformer;
22+
import org.metafacture.fix.parser.RecordTransformerBuilder;
23+
24+
import org.antlr.runtime.ANTLRInputStream;
25+
import org.antlr.runtime.CommonTokenStream;
26+
import org.antlr.runtime.RecognitionException;
27+
import org.antlr.runtime.tree.CommonTreeNodeStream;
28+
29+
import java.io.IOException;
30+
import java.io.InputStream;
31+
import java.util.Map;
32+
33+
/**
34+
* Creates a flow based on a flux script.
35+
*
36+
* @author Fabian Steeg
37+
*/
38+
// This is based on FluxCompiler, but using the ANTLR Fix draft. To be replaced by the Metafix class from metafacture-fix.
39+
// See https://github.com/metafacture/metafacture-fix/blob/master/metafix/src/main/java/org/metafacture/metafix/Metafix.java
40+
public final class Metafix {
41+
private Metafix() {
42+
// no instances
43+
}
44+
45+
/**
46+
* Compiles the Fix to a RecordTransformer.
47+
*
48+
* @see RecordTransformer
49+
* @param fix the Fix
50+
* @param vars the variables of the Fix
51+
* @return the RecordTransformer
52+
* @throws RecognitionException if an ANTLR exception occurs
53+
* @throws IOException if an I/O error occurs
54+
*/
55+
public static RecordTransformer compile(final InputStream fix, final Map<String, String> vars) throws RecognitionException, IOException {
56+
return compileFix(compileAst(fix), vars);
57+
}
58+
59+
private static CommonTreeNodeStream compileAst(final InputStream flowDef) throws IOException, RecognitionException {
60+
final FixParser parser = new FixParser(new CommonTokenStream(new FixLexer(new ANTLRInputStream(flowDef))));
61+
return new CommonTreeNodeStream(parser.fix().getTree());
62+
}
63+
64+
private static RecordTransformer compileFix(final CommonTreeNodeStream treeNodes, final Map<String, String> vars)
65+
throws RecognitionException {
66+
final RecordTransformerBuilder builder = new RecordTransformerBuilder(treeNodes);
67+
return builder.transformer();
68+
}
69+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2024 Fabian Steeg, hbz
3+
*
4+
* Licensed under the Apache License, Version 2.0 the "License";
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.metafacture.fix.parser;
18+
19+
/**
20+
* @author Fabian Steeg
21+
*
22+
*/
23+
// For Flux, the equivalent of this class is `FluxProgramm`. In metafacture-fix, we have RecordTransformer, to be rewritten for ANTLR here.
24+
// See https://github.com/metafacture/metafacture-fix/blob/master/metafix/src/main/java/org/metafacture/metafix/RecordTransformer.java
25+
@SuppressWarnings("checkstyle:MissingCtor")
26+
public final class RecordTransformer {
27+
/**
28+
* @param name The method name
29+
* @param param The param passed to the method
30+
*/
31+
// sample method called from the tree grammar / RecordTransformerBuilder
32+
public void processMethod(final String name, final String param) {
33+
System.out.printf("Called processMethod: %s with %s\n", name, param);
34+
}
35+
36+
}

metafacture-fix/src/test/java/org/metafacture/fix/FixGrammarTest.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.metafacture.fix.parser.FixLexer;
2222
import org.metafacture.fix.parser.FixParser;
23+
import org.metafacture.fix.parser.RecordTransformer;
2324

2425
import org.antlr.runtime.ANTLRInputStream;
2526
import org.antlr.runtime.CommonTokenStream;
@@ -40,18 +41,18 @@ public final class FixGrammarTest {
4041

4142
@Test
4243
public void parseSimpleFix() {
43-
assertEquals("add_field ( test )", parseToString("add_field(test)"));
44+
assertEquals("add_field ( test )", parse("add_field(test)"));
4445
}
4546

4647
@Test
4748
public void parseSimpleFixWithOption() {
48-
assertEquals("add_field ( test1 , option : test2 )", parseToString("add_field(test1,option:test2)"));
49+
assertEquals("add_field ( test1 , option : test2 )", parse("add_field(test1,option:test2)"));
4950
}
5051

5152
@Test
5253
public void parseFixWithIf() {
5354
assertEquals("if some ( test1 ) add ( something ) elsif some ( test2 ) add ( somethingElse , opt : one ) else add ( anotherThing , opt : two ) end",
54-
parseToString(
55+
parse(
5556
"if some(test1)",
5657
" add(something)",
5758
"elsif some(test2)",
@@ -65,21 +66,28 @@ public void parseFixWithIf() {
6566
@Test
6667
public void parseError() {
6768
assertEquals("<unexpected: [@3,14:14='<EOF>',<-1>,1:14], resync=add_field(> <mismatched token: [@3,14:14='<EOF>',<-1>,1:14], resync=test>",
68-
parseToString("add_field(test"));
69+
parse("add_field(test"));
6970
}
7071

71-
private String parseToString(final String... fixLines) {
72+
private String parse(final String... fixLines) {
73+
final String input = String.join("\n", fixLines);
7274
try {
73-
final ByteArrayInputStream fixInput = createInputStream(String.join("\n", fixLines));
74-
final FixLexer lexer = new FixLexer(new ANTLRInputStream(fixInput));
75-
final FixParser parser = new FixParser(new CommonTokenStream(lexer));
76-
return ((Tree) parser.fix().getTree()).toStringTree();
75+
RecordTransformer transformer = Metafix.compile(createInputStream(input), null);
76+
System.out.println("Created RecordTransformer for fix: " + transformer);
77+
return parseToStringTree(input);
7778
} catch (IOException | RecognitionException e) {
7879
e.printStackTrace();
7980
return null;
8081
}
8182
}
8283

84+
private String parseToStringTree(final String input) throws IOException, RecognitionException {
85+
final ByteArrayInputStream fixInput = createInputStream(input);
86+
final FixLexer lexer = new FixLexer(new ANTLRInputStream(fixInput));
87+
final FixParser parser = new FixParser(new CommonTokenStream(lexer));
88+
return ((Tree) parser.fix().getTree()).toStringTree();
89+
}
90+
8391
private ByteArrayInputStream createInputStream(final String string) {
8492
return new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8));
8593
}

0 commit comments

Comments
 (0)