Skip to content

Commit

Permalink
ABNF 名前空間の渡し方をコンストラクタからパラメータに変えた
Browse files Browse the repository at this point in the history
  • Loading branch information
okomeki committed Jan 14, 2022
1 parent 2d57b3f commit 9bdf0a1
Show file tree
Hide file tree
Showing 38 changed files with 286 additions and 193 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>net.siisise</groupId>
<artifactId>softlib-abnf</artifactId>
<name>SoftLibABNF</name>
<version>1.1.2</version>
<version>1.1.3-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
Expand All @@ -30,7 +30,7 @@
<dependency>
<groupId>net.siisise</groupId>
<artifactId>softlib</artifactId>
<version>1.1.2</version>
<version>1.1.3-SNAPSHOT</version>
<type>jar</type>
</dependency>
<dependency>
Expand Down
28 changes: 11 additions & 17 deletions src/main/java/net/siisise/abnf/ABNFReg.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public class ABNFRef extends FindABNF {
}

@Override
public C buildFind(FrontPacket pac, BNFParser... parsers) {
return reg.get(name).find(pac, parsers);
public <X,N> C<X> buildFind(FrontPacket pac, N ns, BNFParser<? extends X>... parsers) {
return reg.get(name).find(pac, ns, parsers);
}

@Override
Expand Down Expand Up @@ -265,7 +265,7 @@ public ABNF.C find(FrontPacket pac, String rulename, String... subrulenames) {

BNFParser[] cll = new BNFParser[subrulenames.length];
for ( int i = 0; i < subrulenames.length; i++ ) {
cll[i] = parser(subrulenames[i], null);
cll[i] = parser(subrulenames[i]);
}
return rule.find(pac, cll);
}
Expand All @@ -280,12 +280,12 @@ public ABNF.C find(FrontPacket pac, String rulename, String... subrulenames) {
* @return 解析後の実体
*/
public <T> T parse(String rulename, String src) {
return (T) parser(rulename, null).parse(src);
return (T) parser(rulename).parse(src);
}

public <T> T parse(String rulename, byte[] src) {
FrontPacket pac = new StreamFrontPacket(new ByteArrayInputStream(src));
return (T) parser(rulename, null).parse(pac);
return (T) parser(rulename).parse(pac);
}

/**
Expand All @@ -298,31 +298,25 @@ public <T> T parse(String rulename, byte[] src) {
* @return 解析後の実体
*/
public <T> T parse(String rulename, FrontPacket pac) {
return (T)parser(rulename, null).parse(pac);
return (T)parser(rulename).parse(pac);
}

/**
*
* @param <T>
* @param rulename
* @param namespace ユーザ空間 ABNF以外ではまだ使ってない 型は変えるかもしれない
* @return
*/
public <T> ABNFParser<T> parser(String rulename, N namespace) {
public <T> ABNFParser<T> parser(String rulename) {
try {
Constructor<? extends ABNFParser> cnst;
ABNF rule = reg.get(rulename);
Class<? extends ABNFParser> rulep = CL.get(rulename);
if ( rulep == null ) {
return (ABNFParser<T>) new ABNFPacketParser(rule);
}
if ( namespace != null ) {
cnst = (Constructor<? extends ABNFParser<T>>) rulep.getConstructor(ABNF.class, namespace.getClass(), ABNFReg.class);
return cnst.newInstance(rule, namespace, this);
} else {
cnst = (Constructor<? extends ABNFParser<T>>) rulep.getConstructor(ABNF.class, ABNFReg.class);
return cnst.newInstance(rule, this);
}
cnst = (Constructor<? extends ABNFParser<T>>) rulep.getConstructor(ABNF.class, ABNFReg.class);
return cnst.newInstance(rule, this);
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException ex) {
throw new java.lang.UnsupportedOperationException(ex);
Expand All @@ -339,7 +333,7 @@ public <T> ABNFParser<T> parser(String rulename, N namespace) {
* @return
*/
private <T> T bnfParse(String rulename, String src) {
return (T) bnfReg.parser(rulename, this).parse(src);
return (T) bnfReg.parser(rulename).parse(src, this);
}

/**
Expand All @@ -350,7 +344,7 @@ private <T> T bnfParse(String rulename, String src) {
* @return
*/
private <T> T bnfParse(String rulename, FrontPacket pac) {
return (T) bnfReg.parser(rulename, this).parse(pac);
return (T) bnfReg.parser(rulename).parse(pac, this);
}

}
11 changes: 8 additions & 3 deletions src/main/java/net/siisise/abnf/ABNFmn.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,22 @@ public ABNFmn copy(ABNFReg reg) {
}

@Override
public Packet is(FrontPacket pac) {
Packet p1 = a.is(pac);
public <N> Packet is(FrontPacket pac, N ns) {
Packet p1 = a.is(pac, ns);
if (p1 == null) {
return null;
}
Packet p2 = b.is(p1);
Packet p2 = b.is(p1, ns);
if (p2 != null) {
pac.dbackWrite(p1.toByteArray());
pac.dbackWrite(p2.toByteArray());
return null;
}
return p1;
}

@Override
public Packet is(FrontPacket pac) {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
}
4 changes: 2 additions & 2 deletions src/main/java/net/siisise/abnf/ABNFor.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ public void add(ABNF... val) {
}

@Override
public <X> C<X> buildFind(FrontPacket pac, BNFParser<? extends X>... parsers) {
public <X,N> C<X> buildFind(FrontPacket pac, N ns, BNFParser<? extends X>... parsers) {
ABNF.C<X> ret = null;

for (ABNF sub : list) {

C<X> subret = sub.find(pac, parsers);
C<X> subret = sub.find(pac, ns, parsers);
if (subret != null) {
byte[] data = subret.ret.toByteArray();
pac.backWrite(data);
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/net/siisise/abnf/ABNFpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,18 @@ public ABNFpl copy(ABNFReg reg) {
/**
*
* @param <X> 返却予定の型
* @param <N> user name space type ユーザ名前空間型
* @param pac 解析対象
* @param ns user name space
* @param parsers サブ解析装置
* @return
*/
@Override
public <X> C<X> buildFind(FrontPacket pac, BNFParser<? extends X>... parsers) {
public <X,N> C<X> buildFind(FrontPacket pac, N ns, BNFParser<? extends X>... parsers) {
C<X> ret = new C<>();

for (ABNF sub : list) {
C<X> subret = sub.find(pac, parsers);
C<X> subret = sub.find(pac, ns, parsers);
if (subret == null) {
pac.dbackWrite(ret.ret.toByteArray());
return null;
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/net/siisise/abnf/ABNFplm.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public ABNFpl copy(ABNFReg reg) {
}

@Override
public <X> C<X> buildFind(FrontPacket pac, BNFParser<? extends X>... subps) {
return longfind(pac, list, subps);
public <X,N> C<X> buildFind(FrontPacket pac, N ns, BNFParser<? extends X>... subps) {
return longfind(pac, ns, list, subps);
}

/**
Expand All @@ -41,7 +41,7 @@ public <X> C<X> buildFind(FrontPacket pac, BNFParser<? extends X>... subps) {
* @param subparsers
* @return
*/
protected <X> C<X> longfind(FrontPacket pac, ABNF[] list, BNFParser<? extends X>[] subparsers) {
protected <X,N> C<X> longfind(FrontPacket pac, N ns, ABNF[] list, BNFParser<? extends X>[] subparsers) {
if (list.length == 0) {
return new C();
}
Expand All @@ -53,7 +53,7 @@ protected <X> C<X> longfind(FrontPacket pac, ABNF[] list, BNFParser<? extends X>
byte[] data = new byte[flen];
pac.read(data, 0, flen);
frontPac.write(data, 0, flen);
C firstret = list[0].find(frontPac, subparsers);
C firstret = list[0].find(frontPac, ns, subparsers);
pac.dbackWrite(frontPac.toByteArray());

if (firstret == null || list.length == 1) { // 一致しないか最後ならここで戻り
Expand All @@ -63,7 +63,7 @@ protected <X> C<X> longfind(FrontPacket pac, ABNF[] list, BNFParser<? extends X>
// 2つめ以降
ABNF[] slist = new ABNF[list.length - 1];
System.arraycopy(list, 1, slist, 0, slist.length);
C nextret = longfind(pac, slist, subparsers);
C nextret = longfind(pac, ns, slist, subparsers);
if (nextret != null) {
// firstret と nextret 両方成立
mix(firstret, nextret);
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/net/siisise/abnf/ABNFplu.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ public ABNFpl copy(ABNFReg reg) {
* 詳細検索
*
* @param <X>
* @param <N>
* @param pac
* @param ns
* @param list
* @param subparsers
* @return
*/
@Override
protected <X> C<X> longfind(FrontPacket pac, ABNF[] list, BNFParser<? extends X>[] subparsers) {
protected <X,N> C<X> longfind(FrontPacket pac, N ns, ABNF[] list, BNFParser<? extends X>[] subparsers) {
if (list.length == 0) {
return new C();
}
Expand All @@ -47,7 +49,7 @@ protected <X> C<X> longfind(FrontPacket pac, ABNF[] list, BNFParser<? extends X>
byte[] data = new byte[flen];
pac.read(data, 0, flen);
frontPac.write(data, 0, flen);
C<X> firstret = list[0].find(frontPac, subparsers);
C<X> firstret = list[0].find(frontPac, ns, subparsers);
pac.dbackWrite(frontPac.toByteArray());

if (firstret == null || list.length == 1) { // 一致しないか最後ならここで戻り
Expand All @@ -57,7 +59,7 @@ protected <X> C<X> longfind(FrontPacket pac, ABNF[] list, BNFParser<? extends X>
// 2つめ以降
ABNF[] slist = new ABNF[list.length - 1];
System.arraycopy(list, 1, slist, 0, slist.length);
C nextret = longfind(pac, slist, subparsers);
C nextret = longfind(pac, ns, slist, subparsers);
if (nextret != null) {
// firstret と nextret 両方成立
mix(firstret, nextret);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/siisise/abnf/ABNFx.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ public ABNFx copy(ABNFReg reg) {
}

@Override
public <X> C<X> buildFind(FrontPacket pac, BNFParser<? extends X>... names) {
public <X,N> C<X> buildFind(FrontPacket pac, N ns, BNFParser<? extends X>... names) {
C<X> ret = new C<>();
for (int i = 0; b == -1 || i < b; i++) {
// System.out.println(abnf+":" + strd(ret.ret)+"%"+strd(pac));
C sub = abnf.find(pac, names);
C sub = abnf.find(pac, ns, names);
if (sub == null) {
if (i < a) {
byte[] data = ret.ret.toByteArray();
Expand Down
20 changes: 15 additions & 5 deletions src/main/java/net/siisise/abnf/FindABNF.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,36 @@ public abstract class FindABNF extends AbstractABNF {

@Override
public Packet is(FrontPacket src) {
C ret = find(src);
return is(src, null);
}

@Override
public <N> Packet is(FrontPacket src, N ns) {
C ret = find(src, ns);
if (ret == null) {
return null;
}
return ret.ret;
}

public <X> C<X> find(FrontPacket pac, BNFParser<? extends X>... parsers) {
return find(pac, null, parsers);
}

/**
* 詰め方の工夫をするターン
*
* @param <X>
* @param pac
* @param ns
* @param parsers
* @return
*/
@Override
public <X> C<X> find(FrontPacket pac, BNFParser<? extends X>... parsers) {
public <X,N> C<X> find(FrontPacket pac, N ns, BNFParser<? extends X>... parsers) {
BNFParser<? extends X> mp = matchParser(parsers);
C<X> ret = buildFind(pac, mp == null ? parsers : new BNFParser[0]);
return ret != null ? subBuild(ret, mp) : null;
C<X> ret = buildFind(pac, ns, mp == null ? parsers : new BNFParser[0]);
return ret != null ? subBuild(ret, ns, mp) : null;
}

/**
Expand All @@ -42,5 +52,5 @@ public <X> C<X> find(FrontPacket pac, BNFParser<? extends X>... parsers) {
* @param parsers
* @return
*/
abstract <X> C<X> buildFind(FrontPacket pac, BNFParser<? extends X>... parsers);
abstract <X,N> C<X> buildFind(FrontPacket pac, N ns, BNFParser<? extends X>... parsers);
}
12 changes: 9 additions & 3 deletions src/main/java/net/siisise/abnf/IsABNF.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,26 @@
* サブ要素を持たない方
*/
public abstract class IsABNF extends AbstractABNF {

@Override
public <N> Packet is(FrontPacket pac, N ns) {
return is(pac);
}

/**
* sub要素のない場合の軽い対応
* @param <X>
* @param pac
* @param ns
* @param parsers
* @return
*/
@Override
public <X> C<X> find(FrontPacket pac, BNFParser<? extends X>... parsers) {
Packet r = is(pac);
public <X,N> C<X> find(FrontPacket pac, N ns, BNFParser<? extends X>... parsers) {
Packet r = is(pac, ns);
if (r == null) {
return null;
}
return subBuild(new C(r), matchParser(parsers));
return subBuild(new C(r), ns, matchParser(parsers));
}
}
Loading

0 comments on commit 9bdf0a1

Please sign in to comment.