Skip to content

Commit

Permalink
axiom: Introduced base parser and stream APIs
Browse files Browse the repository at this point in the history
Signed-off-by: Tony Tkacik <tonydamage@gmail.com>
  • Loading branch information
tonydamage committed Apr 30, 2020
1 parent 73ec8e0 commit 95577d4
Show file tree
Hide file tree
Showing 22 changed files with 921 additions and 0 deletions.
81 changes: 81 additions & 0 deletions infra/axiom/pom.xml
@@ -0,0 +1,81 @@
<?xml version="1.0"?>
<!--
~ Copyright (c) 2010-2018 Evolveum and contributors
~
~ This work is dual-licensed under the Apache License 2.0
~ and European Union Public License. See LICENSE file for details.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>infra</artifactId>
<groupId>com.evolveum.midpoint.infra</groupId>
<version>4.2-SNAPSHOT</version>
</parent>

<groupId>com.evolveum.axiom</groupId>
<artifactId>axiom</artifactId>

<name>Axiom</name>

<properties>
<maven.test.skip>true</maven.test.skip>
</properties>

<dependencies>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>4.8-1</version>
</dependency>
<!-- test dependencies -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.evolveum.midpoint.tools</groupId>
<artifactId>test-ng</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.8-1</version>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
<configuration>
<visitor>true</visitor>
<listener>true</listener>
</configuration>
</plugin>
</plugins>
</build>
</project>
45 changes: 45 additions & 0 deletions infra/axiom/src/main/antlr4/com/evolveum/axiom/lang/antlr/Axiom.g4
@@ -0,0 +1,45 @@
grammar Axiom;

SEMICOLON : ';';
LEFT_BRACE : '{';
RIGHT_BRACE : '}';
COLON : ':';
PLUS : '+';
LINE_COMMENT : [ \n\r\t]* ('//' (~[\r\n]*)) [ \n\r\t]* -> skip;
SEP: [ \n\r\t]+;
IDENTIFIER : [a-zA-Z_/][a-zA-Z0-9_\-./]*;

fragment SQOUTE : '\'';
fragment DQOUTE : '"';


//fragment SUB_STRING : ('"' (ESC | ~["])*? '"') | ('\'' (ESC | ~['])* '\'');
//fragment ESC : '\\' (["\\/bfnrt] | UNICODE);
//fragment UNICODE : 'u' HEX HEX HEX HEX;
//fragment HEX : [0-9a-fA-F] ;
//STRING: ((~( '\r' | '\n' | '\t' | ' ' | ';' | '{' | '"' | '\'' | '}' | '/' | '+')~( '\r' | '\n' | '\t' | ' ' | ';' | '{' | '}' )* ) | SUB_STRING );

fragment ESC : '\\';

STRING_SINGLEQUOTE: SQOUTE ((ESC SQOUTE) | ~[\n'])* SQOUTE;
STRING_DOUBLEQUOTE: DQOUTE ((ESC DQOUTE) | ~[\n"])* DQOUTE;
//STRING_MULTILINE: '"""' (ESC | ~('"""'))* '"""';
statement : SEP* identifier SEP* (argument)? SEP* (SEMICOLON | LEFT_BRACE SEP* (statement)* SEP* RIGHT_BRACE SEP*) SEP*;
identifier : (prefix COLON)? localIdentifier;
prefix : IDENTIFIER;
localIdentifier : IDENTIFIER;
// argument : STRING (SEP* PLUS SEP* STRING)* | IDENTIFIER;
argument : identifier | string;
string : singleQuoteString | doubleQuoteString | multilineString;
singleQuoteString : STRING_SINGLEQUOTE;
doubleQuoteString : STRING_DOUBLEQUOTE;
multilineString: '"""\n' (~('"""'))*'"""';
@@ -0,0 +1,68 @@
package com.evolveum.axiom.api;

import java.util.Objects;

import com.google.common.base.Preconditions;

public class AxiomIdentifier {

public static final String AXIOM_NAMESPACE = "https://ns.evolveum.com/axiom";
private final String namespace;
private final String localName;

public AxiomIdentifier(String namespace, String localName) {
this.namespace = Preconditions.checkNotNull(namespace, "namespace");
this.localName = Preconditions.checkNotNull(localName, "localName");
}

public String getNamespace() {
return namespace;
}

public String getLocalName() {
return localName;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((localName == null) ? 0 : localName.hashCode());
result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!(obj instanceof AxiomIdentifier))
return false;
AxiomIdentifier other = (AxiomIdentifier) obj;
if (localName == null) {
if (other.localName != null)
return false;
} else if (!localName.equals(other.localName))
return false;
if (namespace == null) {
if (other.namespace != null)
return false;
} else if (!namespace.equals(other.namespace))
return false;
return true;
}

@Override
public String toString() {
return localName;
}

public static AxiomIdentifier axiom(String identifier) {
return new AxiomIdentifier(AXIOM_NAMESPACE, identifier);
}

public static AxiomIdentifier from(String namespace, String localName) {
return new AxiomIdentifier(namespace, localName);
}

}
@@ -0,0 +1,29 @@
package com.evolveum.axiom.lang.api;

import com.evolveum.axiom.api.AxiomIdentifier;

class AxiomBuiltInProperty implements AxiomPropertyDefinition {

private final AxiomIdentifier argument;
private final AxiomTypeDefinition type;

public AxiomBuiltInProperty(AxiomIdentifier argument, AxiomTypeDefinition type) {
this.argument = argument;
this.type = type;
}

@Override
public AxiomIdentifier getIdentifier() {
return argument;
}

@Override
public AxiomTypeDefinition getType() {
return type;
}

@Override
public boolean required() {
return true;
}
}
@@ -0,0 +1,20 @@
package com.evolveum.axiom.lang.api;

import com.evolveum.axiom.api.AxiomIdentifier;

public enum AxiomBuiltInSimpleType implements AxiomTypeDefinition {

IDENTIFIER("identifier"),
STRING("string"),
SEMANTIC_VERSION("SemanticVersion");

private final AxiomIdentifier identifier;

AxiomBuiltInSimpleType(String identifier) {
this.identifier = AxiomIdentifier.axiom(identifier);
}

public AxiomIdentifier getIdentifier() {
return identifier;
}
}
@@ -0,0 +1,10 @@
package com.evolveum.axiom.lang.api;

import com.evolveum.axiom.api.AxiomIdentifier;

public interface AxiomPropertyDefinition {

AxiomIdentifier getIdentifier();
AxiomTypeDefinition getType();
boolean required();
}
@@ -0,0 +1,16 @@
package com.evolveum.axiom.lang.api;

import java.util.Collection;

import com.evolveum.axiom.api.AxiomIdentifier;

public interface AxiomStatement<V> {

AxiomIdentifier keyword();
V value();

Collection<AxiomStatement<?>> children();
Collection<AxiomStatement<?>> children(AxiomIdentifier type);


}
@@ -0,0 +1,11 @@
package com.evolveum.axiom.lang.api;

import com.evolveum.axiom.api.AxiomIdentifier;

public interface AxiomStatementStreamListener {

void startStatement(AxiomIdentifier statement);
void argument(AxiomIdentifier identifier);
void argument(String identifier);
void endStatement();
}
@@ -0,0 +1,5 @@
package com.evolveum.axiom.lang.api;

public interface AxiomTypeDefinition {

}
@@ -0,0 +1,60 @@
package com.evolveum.axiom.lang.impl;

import com.evolveum.axiom.api.AxiomIdentifier;
import com.evolveum.axiom.lang.antlr.AxiomBaseListener;
import com.evolveum.axiom.lang.antlr.AxiomParser.ArgumentContext;
import com.evolveum.axiom.lang.antlr.AxiomParser.DoubleQuoteStringContext;
import com.evolveum.axiom.lang.antlr.AxiomParser.IdentifierContext;
import com.evolveum.axiom.lang.antlr.AxiomParser.StatementContext;
import com.evolveum.axiom.lang.api.AxiomStatementStreamListener;

public class AxiomAntlrAdapter extends AxiomBaseListener {

private final AxiomIdentifierResolver statements;
private final AxiomStatementStreamListener delegate;



public AxiomAntlrAdapter(AxiomIdentifierResolver statements, AxiomStatementStreamListener delegate) {
this.statements = statements;
this.delegate = delegate;
}



@Override
public void enterStatement(StatementContext ctx) {
AxiomIdentifier identifier = statementIdentifier(ctx.identifier());
delegate.startStatement(identifier);
super.enterStatement(ctx);
}

@Override
public void enterArgument(ArgumentContext ctx) {
if (ctx.identifier() != null) {
enterArgument(ctx.identifier());
} else {
enterStringArgument(ctx.identifier());
}



super.enterArgument(ctx);
}

private void enterStringArgument(IdentifierContext identifier) {
// TODO Auto-generated method stub

}

private void enterArgument(IdentifierContext identifier) {
delegate.argument(statementIdentifier(identifier));
}

private AxiomIdentifier statementIdentifier(IdentifierContext identifier) {
String prefix = identifier.prefix().getText();
String localName = identifier.localIdentifier().getText();
return statements.resolveStatementIdentifier(prefix,localName);
}

}

0 comments on commit 95577d4

Please sign in to comment.