Skip to content

Commit

Permalink
add abnf basic structure
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-toepfer committed Nov 4, 2023
1 parent 7b77e1f commit 93cae78
Show file tree
Hide file tree
Showing 53 changed files with 3,710 additions and 14 deletions.
13 changes: 5 additions & 8 deletions vocabulary-format-assertion/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
<version>5.6.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
<version>5.6.0</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
Expand All @@ -43,6 +41,11 @@
<artifactId>hamcrest-optional</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>json-schema-core</artifactId>
Expand All @@ -61,11 +64,5 @@
<artifactId>parsson</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.6.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
* The MIT License
*
* Copyright 2023 sebastian.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf;

import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.Alternative;
import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.Element;
import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.NumericCharacter;
import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.RuleName;
import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.StringElement;
import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.ValueRangeAlternatives;

public enum CoreRules implements Element {
ALPHA() {
@Override
public boolean isValidFor(final int codePoint) {
return Alternative
.of(
ValueRangeAlternatives.of(
NumericCharacter.of(NumericCharacter.BASE.HEXADECIMAL, 0x41),
NumericCharacter.of(NumericCharacter.BASE.HEXADECIMAL, 0x51A)
),
ValueRangeAlternatives.of(
NumericCharacter.of(NumericCharacter.BASE.HEXADECIMAL, 0x61),
NumericCharacter.of(NumericCharacter.BASE.HEXADECIMAL, 0x7A)
)
)
.isValidFor(codePoint);
}
},
BIT() {
@Override
public boolean isValidFor(final int codePoint) {
return Alternative.of(StringElement.of("0"), StringElement.of("1")).isValidFor(codePoint);
}
},
CHAR() {
@Override
public boolean isValidFor(final int codePoint) {
return ValueRangeAlternatives
.of(
NumericCharacter.of(NumericCharacter.BASE.HEXADECIMAL, 0x01),
NumericCharacter.of(NumericCharacter.BASE.HEXADECIMAL, 0x7F)
)
.isValidFor(codePoint);
}
},
CR() {
@Override
public boolean isValidFor(final int codePoint) {
return NumericCharacter.of(NumericCharacter.BASE.HEXADECIMAL, 0x0D).isValidFor(codePoint);
}
},
CRLF, //CR LF
CTL, //%x00-1F / %x7F
DIGIT() {
@Override
public boolean isValidFor(final int codePoint) {
return ValueRangeAlternatives
.of(
NumericCharacter.of(NumericCharacter.BASE.HEXADECIMAL, 0x30),
NumericCharacter.of(NumericCharacter.BASE.HEXADECIMAL, 0x39)
)
.isValidFor(codePoint);
}
},
DQUOTE() {
@Override
public boolean isValidFor(final int codePoint) {
return NumericCharacter.of(NumericCharacter.BASE.HEXADECIMAL, 0x22).isValidFor(codePoint);
}
},
HEXDIG() {
@Override
public boolean isValidFor(final int codePoint) {
return Alternative
.of(
DIGIT,
StringElement.of("A"),
StringElement.of("B"),
StringElement.of("C"),
StringElement.of("D"),
StringElement.of("E"),
StringElement.of("F")
)
.isValidFor(codePoint);
}
},
HTAB() {
@Override
public boolean isValidFor(final int codePoint) {
return NumericCharacter.of(NumericCharacter.BASE.HEXADECIMAL, 0x09).isValidFor(codePoint);
}
},
LF() {
@Override
public boolean isValidFor(final int codePoint) {
return NumericCharacter.of(NumericCharacter.BASE.HEXADECIMAL, 0x0A).isValidFor(codePoint);
}
},
LWSP, //*(WSP / CRLF WSP)
OCTET() {
@Override
public boolean isValidFor(final int codePoint) {
return ValueRangeAlternatives
.of(
NumericCharacter.of(NumericCharacter.BASE.HEXADECIMAL, 0x00),
NumericCharacter.of(NumericCharacter.BASE.HEXADECIMAL, 0xFF)
)
.isValidFor(codePoint);
}
},
SP() {
@Override
public boolean isValidFor(final int codePoint) {
return NumericCharacter.of(NumericCharacter.BASE.HEXADECIMAL, 0x20).isValidFor(codePoint);
}
},
VCHAR() {
@Override
public boolean isValidFor(final int codePoint) {
return ValueRangeAlternatives
.of(
NumericCharacter.of(NumericCharacter.BASE.HEXADECIMAL, 0x21),
NumericCharacter.of(NumericCharacter.BASE.HEXADECIMAL, 0x7E)
)
.isValidFor(codePoint);
}
},
WSP {
@Override
public boolean isValidFor(final int codePoint) {
return Alternative.of(SP, HTAB).isValidFor(codePoint);
}
};

public RuleName asRuleName() {
return RuleName.of(name());
}

@Override
public boolean isValidFor(final int codePoint) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* The MIT License
*
* Copyright 2023 sebastian.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf;

import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.Element;
import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.RuleName;
import java.util.Objects;

public final class Rule {

public static Rule of(final RuleName name, final Element elements) {
return new Rule(name, elements);
}

private final RuleName name;
private final Element elements;

private Rule(final RuleName name, final Element elements) {
this.name = Objects.requireNonNull(name);
this.elements = Objects.requireNonNull(elements);
}

public boolean hasRuleName(final RuleName name) {
return Objects.equals(this.name, name);
}

@Override
public String toString() {
return "Rule{" + "name=" + name + ", elements=" + elements + '}';
}

@Override
public int hashCode() {
int hash = 5;
hash = 83 * hash + Objects.hashCode(this.name);
hash = 83 * hash + Objects.hashCode(this.elements);
return hash;
}

@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Rule other = (Rule) obj;
if (!Objects.equals(this.name, other.name)) {
return false;
}
return Objects.equals(this.elements, other.elements);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* The MIT License
*
* Copyright 2023 sebastian.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf;

import static java.util.Arrays.asList;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public final class RuleList {

public static RuleList of(final Rule rule, final Rule... rules) {
final List<Rule> ruleList = new ArrayList<>();
ruleList.add(Objects.requireNonNull(rule));
ruleList.addAll(asList(rules));
return of(ruleList);
}

public static RuleList of(final List<Rule> rules) {
return new RuleList(rules);
}

private final List<Rule> rules;

private RuleList(final List<Rule> rules) {
this.rules = List.copyOf(rules);
}

@Override
public String toString() {
return "RuleList{" + "rules=" + rules + '}';
}

@Override
public int hashCode() {
int hash = 7;
hash = 73 * hash + Objects.hashCode(this.rules);
return hash;
}

@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final RuleList other = (RuleList) obj;
return Objects.equals(this.rules, other.rules);
}
}
Loading

0 comments on commit 93cae78

Please sign in to comment.