Mercurial Hosting > luan
changeset 1111:88b5b81cad4a
move Parser to luan.lib.parser
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 02 Aug 2017 15:19:47 -0600 |
parents | 38a42f437fd2 |
children | 490f77bb2ad1 |
files | src/luan/lib/parser/ParseException.java src/luan/lib/parser/Parser.java src/luan/modules/RpcLuan.java src/luan/modules/lucene/LuceneIndex.java src/luan/modules/lucene/queryparser/FieldParser.java src/luan/modules/lucene/queryparser/MultiFieldParser.java src/luan/modules/lucene/queryparser/NumberFieldParser.java src/luan/modules/lucene/queryparser/SaneQueryParser.java src/luan/modules/lucene/queryparser/StringFieldParser.java src/luan/modules/lucene/queryparser/SynonymParser.java src/luan/modules/parsers/BBCode.java src/luan/modules/parsers/Csv.java src/luan/modules/parsers/Html.java src/luan/modules/parsers/Json.java src/luan/modules/parsers/ParseException.java src/luan/modules/parsers/Parser.java src/luan/modules/parsers/Theme.java |
diffstat | 17 files changed, 236 insertions(+), 227 deletions(-) [+] |
line wrap: on
line diff
diff -r 38a42f437fd2 -r 88b5b81cad4a src/luan/lib/parser/ParseException.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/lib/parser/ParseException.java Wed Aug 02 15:19:47 2017 -0600 @@ -0,0 +1,62 @@ +package luan.lib.parser; + + +public final class ParseException extends Exception { + public final String text; + public final int errorIndex; + public final int highIndex; + + public ParseException(Parser parser,String msg) { + super(msg); + this.text = parser.text; + this.errorIndex = parser.currentIndex(); + this.highIndex = parser.highIndex(); + } + + public ParseException(Parser parser,Exception cause) { + this(parser,cause.getMessage(),cause); + } + + public ParseException(Parser parser,String msg,Exception cause) { + super(msg,cause); + this.text = parser.text; + this.errorIndex = parser.currentIndex(); + this.highIndex = parser.highIndex(); + } + + private class Location { + final int line; + final int pos; + + Location(int index) { + int line = 0; + int i = -1; + while(true) { + int j = text.indexOf('\n',i+1); + if( j == -1 || j >= index ) + break; + i = j; + line++; + } + this.line = line; + this.pos = index - i - 1; + } + } + + private String[] lines() { + return text.split("\n",-1); + } + + @Override public String getMessage() { + Location loc = new Location(errorIndex); + String line = lines()[loc.line]; + String msg = super.getMessage() + " (line " + (loc.line+1) + ", pos " + (loc.pos+1) + ")\n"; + StringBuilder sb = new StringBuilder(msg); + sb.append( line + "\n" ); + for( int i=0; i<loc.pos; i++ ) { + sb.append( line.charAt(i)=='\t' ? '\t' : ' ' ); + } + sb.append("^\n"); + return sb.toString(); + } +}
diff -r 38a42f437fd2 -r 88b5b81cad4a src/luan/lib/parser/Parser.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/lib/parser/Parser.java Wed Aug 02 15:19:47 2017 -0600 @@ -0,0 +1,156 @@ +package luan.lib.parser; + + +public class Parser { + public final String text; + private final int len; + private int[] stack = new int[256]; + private int frame = 0; + private int iHigh; + + public Parser(String text) { + this.text = text; + this.len = text.length(); + } + + private int i() { + return stack[frame]; + } + + private void i(int i) { + stack[frame] += i; + if( iHigh < stack[frame] ) + iHigh = stack[frame]; + } + + public int begin() { + frame++; + if( frame == stack.length ) { + int[] a = new int[2*frame]; + System.arraycopy(stack,0,a,0,frame); + stack = a; + } + stack[frame] = stack[frame-1]; + return i(); + } + + public void rollback() { + stack[frame] = stack[frame-1]; + } + + public <T> T success(T t) { + success(); + return t; + } + + public boolean success() { + frame--; + stack[frame] = stack[frame+1]; + return true; + } + + public <T> T failure(T t) { + failure(); + return t; + } + + public boolean failure() { + frame--; + return false; + } + + public int currentIndex() { + return i(); + } +/* + public int errorIndex() { + return frame > 0 ? stack[frame-1] : 0; + } +*/ + public int highIndex() { + return iHigh; + } + + public char lastChar() { + return text.charAt(i()-1); + } + + public char currentChar() { + return text.charAt(i()); + } + + public boolean endOfInput() { + return i() >= len; + } + + public boolean match(char c) { + if( endOfInput() || text.charAt(i()) != c ) + return false; + i(1); + return true; + } + + public boolean match(String s) { + int n = s.length(); + if( !text.regionMatches(i(),s,0,n) ) + return false; + i(n); + return true; + } + + public boolean matchIgnoreCase(String s) { + int n = s.length(); + if( !text.regionMatches(true,i(),s,0,n) ) + return false; + i(n); + return true; + } + + public boolean anyOf(String s) { + if( endOfInput() || s.indexOf(text.charAt(i())) == -1 ) + return false; + i(1); + return true; + } + + public boolean noneOf(String s) { + if( endOfInput() || s.indexOf(text.charAt(i())) != -1 ) + return false; + i(1); + return true; + } + + public boolean inCharRange(char cLow, char cHigh) { + if( endOfInput() ) + return false; + char c = text.charAt(i()); + if( !(cLow <= c && c <= cHigh) ) + return false; + i(1); + return true; + } + + public boolean anyChar() { + if( endOfInput() ) + return false; + i(1); + return true; + } + + public boolean test(char c) { + return !endOfInput() && text.charAt(i()) == c; + } + + public boolean test(String s) { + return text.regionMatches(i(),s,0,s.length()); + } + + public boolean testIgnoreCase(String s) { + return text.regionMatches(true,i(),s,0,s.length()); + } + + public String textFrom(int start) { + return text.substring(start,i()); + } + +}
diff -r 38a42f437fd2 -r 88b5b81cad4a src/luan/modules/RpcLuan.java --- a/src/luan/modules/RpcLuan.java Wed Aug 02 13:45:06 2017 -0600 +++ b/src/luan/modules/RpcLuan.java Wed Aug 02 15:19:47 2017 -0600 @@ -21,7 +21,7 @@ import luan.LuanException; import luan.LuanMethod; import luan.modules.parsers.Json; -import luan.modules.parsers.ParseException; +import luan.lib.parser.ParseException; public final class RpcLuan {
diff -r 38a42f437fd2 -r 88b5b81cad4a src/luan/modules/lucene/LuceneIndex.java --- a/src/luan/modules/lucene/LuceneIndex.java Wed Aug 02 13:45:06 2017 -0600 +++ b/src/luan/modules/lucene/LuceneIndex.java Wed Aug 02 15:19:47 2017 -0600 @@ -64,7 +64,7 @@ import luan.modules.lucene.queryparser.MultiFieldParser; import luan.modules.lucene.queryparser.StringFieldParser; import luan.modules.lucene.queryparser.NumberFieldParser; -import luan.modules.parsers.ParseException; +import luan.lib.parser.ParseException; import luan.modules.Utils; import luan.Luan; import luan.LuanState;
diff -r 38a42f437fd2 -r 88b5b81cad4a src/luan/modules/lucene/queryparser/FieldParser.java --- a/src/luan/modules/lucene/queryparser/FieldParser.java Wed Aug 02 13:45:06 2017 -0600 +++ b/src/luan/modules/lucene/queryparser/FieldParser.java Wed Aug 02 15:19:47 2017 -0600 @@ -2,7 +2,7 @@ import org.apache.lucene.search.Query; import org.apache.lucene.search.SortField; -import luan.modules.parsers.ParseException; +import luan.lib.parser.ParseException; public interface FieldParser {
diff -r 38a42f437fd2 -r 88b5b81cad4a src/luan/modules/lucene/queryparser/MultiFieldParser.java --- a/src/luan/modules/lucene/queryparser/MultiFieldParser.java Wed Aug 02 13:45:06 2017 -0600 +++ b/src/luan/modules/lucene/queryparser/MultiFieldParser.java Wed Aug 02 15:19:47 2017 -0600 @@ -6,7 +6,7 @@ import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.SortField; -import luan.modules.parsers.ParseException; +import luan.lib.parser.ParseException; public class MultiFieldParser implements FieldParser {
diff -r 38a42f437fd2 -r 88b5b81cad4a src/luan/modules/lucene/queryparser/NumberFieldParser.java --- a/src/luan/modules/lucene/queryparser/NumberFieldParser.java Wed Aug 02 13:45:06 2017 -0600 +++ b/src/luan/modules/lucene/queryparser/NumberFieldParser.java Wed Aug 02 15:19:47 2017 -0600 @@ -3,7 +3,7 @@ import org.apache.lucene.search.Query; import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.SortField; -import luan.modules.parsers.ParseException; +import luan.lib.parser.ParseException; public abstract class NumberFieldParser implements FieldParser {
diff -r 38a42f437fd2 -r 88b5b81cad4a src/luan/modules/lucene/queryparser/SaneQueryParser.java --- a/src/luan/modules/lucene/queryparser/SaneQueryParser.java Wed Aug 02 13:45:06 2017 -0600 +++ b/src/luan/modules/lucene/queryparser/SaneQueryParser.java Wed Aug 02 15:19:47 2017 -0600 @@ -9,8 +9,8 @@ import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.Sort; import org.apache.lucene.search.SortField; -import luan.modules.parsers.Parser; -import luan.modules.parsers.ParseException; +import luan.lib.parser.Parser; +import luan.lib.parser.ParseException; public class SaneQueryParser {
diff -r 38a42f437fd2 -r 88b5b81cad4a src/luan/modules/lucene/queryparser/StringFieldParser.java --- a/src/luan/modules/lucene/queryparser/StringFieldParser.java Wed Aug 02 13:45:06 2017 -0600 +++ b/src/luan/modules/lucene/queryparser/StringFieldParser.java Wed Aug 02 15:19:47 2017 -0600 @@ -14,7 +14,7 @@ import org.apache.lucene.search.PrefixQuery; import org.apache.lucene.search.SortField; import org.apache.lucene.index.Term; -import luan.modules.parsers.ParseException; +import luan.lib.parser.ParseException; public class StringFieldParser implements FieldParser {
diff -r 38a42f437fd2 -r 88b5b81cad4a src/luan/modules/lucene/queryparser/SynonymParser.java --- a/src/luan/modules/lucene/queryparser/SynonymParser.java Wed Aug 02 13:45:06 2017 -0600 +++ b/src/luan/modules/lucene/queryparser/SynonymParser.java Wed Aug 02 15:19:47 2017 -0600 @@ -5,7 +5,7 @@ import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.SortField; -import luan.modules.parsers.ParseException; +import luan.lib.parser.ParseException; public class SynonymParser implements FieldParser {
diff -r 38a42f437fd2 -r 88b5b81cad4a src/luan/modules/parsers/BBCode.java --- a/src/luan/modules/parsers/BBCode.java Wed Aug 02 13:45:06 2017 -0600 +++ b/src/luan/modules/parsers/BBCode.java Wed Aug 02 15:19:47 2017 -0600 @@ -7,6 +7,7 @@ import luan.LuanFunction; import luan.LuanException; import luan.modules.Utils; +import luan.lib.parser.Parser; public final class BBCode {
diff -r 38a42f437fd2 -r 88b5b81cad4a src/luan/modules/parsers/Csv.java --- a/src/luan/modules/parsers/Csv.java Wed Aug 02 13:45:06 2017 -0600 +++ b/src/luan/modules/parsers/Csv.java Wed Aug 02 15:19:47 2017 -0600 @@ -1,6 +1,8 @@ package luan.modules.parsers; import luan.LuanTable; +import luan.lib.parser.Parser; +import luan.lib.parser.ParseException; public final class Csv {
diff -r 38a42f437fd2 -r 88b5b81cad4a src/luan/modules/parsers/Html.java --- a/src/luan/modules/parsers/Html.java Wed Aug 02 13:45:06 2017 -0600 +++ b/src/luan/modules/parsers/Html.java Wed Aug 02 15:19:47 2017 -0600 @@ -5,6 +5,8 @@ import java.util.Set; import java.util.HashSet; import luan.LuanTable; +import luan.lib.parser.Parser; +import luan.lib.parser.ParseException; public final class Html {
diff -r 38a42f437fd2 -r 88b5b81cad4a src/luan/modules/parsers/Json.java --- a/src/luan/modules/parsers/Json.java Wed Aug 02 13:45:06 2017 -0600 +++ b/src/luan/modules/parsers/Json.java Wed Aug 02 15:19:47 2017 -0600 @@ -7,6 +7,8 @@ import java.util.Iterator; import luan.LuanTable; import luan.LuanException; +import luan.lib.parser.Parser; +import luan.lib.parser.ParseException; public final class Json {
diff -r 38a42f437fd2 -r 88b5b81cad4a src/luan/modules/parsers/ParseException.java --- a/src/luan/modules/parsers/ParseException.java Wed Aug 02 13:45:06 2017 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,62 +0,0 @@ -package luan.modules.parsers; - - -public final class ParseException extends Exception { - public final String text; - public final int errorIndex; - public final int highIndex; - - public ParseException(Parser parser,String msg) { - super(msg); - this.text = parser.text; - this.errorIndex = parser.currentIndex(); - this.highIndex = parser.highIndex(); - } - - public ParseException(Parser parser,Exception cause) { - this(parser,cause.getMessage(),cause); - } - - public ParseException(Parser parser,String msg,Exception cause) { - super(msg,cause); - this.text = parser.text; - this.errorIndex = parser.currentIndex(); - this.highIndex = parser.highIndex(); - } - - private class Location { - final int line; - final int pos; - - Location(int index) { - int line = 0; - int i = -1; - while(true) { - int j = text.indexOf('\n',i+1); - if( j == -1 || j >= index ) - break; - i = j; - line++; - } - this.line = line; - this.pos = index - i - 1; - } - } - - private String[] lines() { - return text.split("\n",-1); - } - - @Override public String getMessage() { - Location loc = new Location(errorIndex); - String line = lines()[loc.line]; - String msg = super.getMessage() + " (line " + (loc.line+1) + ", pos " + (loc.pos+1) + ")\n"; - StringBuilder sb = new StringBuilder(msg); - sb.append( line + "\n" ); - for( int i=0; i<loc.pos; i++ ) { - sb.append( line.charAt(i)=='\t' ? '\t' : ' ' ); - } - sb.append("^\n"); - return sb.toString(); - } -}
diff -r 38a42f437fd2 -r 88b5b81cad4a src/luan/modules/parsers/Parser.java --- a/src/luan/modules/parsers/Parser.java Wed Aug 02 13:45:06 2017 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,156 +0,0 @@ -package luan.modules.parsers; - - -public class Parser { - public final String text; - private final int len; - private int[] stack = new int[256]; - private int frame = 0; - private int iHigh; - - public Parser(String text) { - this.text = text; - this.len = text.length(); - } - - private int i() { - return stack[frame]; - } - - private void i(int i) { - stack[frame] += i; - if( iHigh < stack[frame] ) - iHigh = stack[frame]; - } - - public int begin() { - frame++; - if( frame == stack.length ) { - int[] a = new int[2*frame]; - System.arraycopy(stack,0,a,0,frame); - stack = a; - } - stack[frame] = stack[frame-1]; - return i(); - } - - public void rollback() { - stack[frame] = stack[frame-1]; - } - - public <T> T success(T t) { - success(); - return t; - } - - public boolean success() { - frame--; - stack[frame] = stack[frame+1]; - return true; - } - - public <T> T failure(T t) { - failure(); - return t; - } - - public boolean failure() { - frame--; - return false; - } - - public int currentIndex() { - return i(); - } -/* - public int errorIndex() { - return frame > 0 ? stack[frame-1] : 0; - } -*/ - public int highIndex() { - return iHigh; - } - - public char lastChar() { - return text.charAt(i()-1); - } - - public char currentChar() { - return text.charAt(i()); - } - - public boolean endOfInput() { - return i() >= len; - } - - public boolean match(char c) { - if( endOfInput() || text.charAt(i()) != c ) - return false; - i(1); - return true; - } - - public boolean match(String s) { - int n = s.length(); - if( !text.regionMatches(i(),s,0,n) ) - return false; - i(n); - return true; - } - - public boolean matchIgnoreCase(String s) { - int n = s.length(); - if( !text.regionMatches(true,i(),s,0,n) ) - return false; - i(n); - return true; - } - - public boolean anyOf(String s) { - if( endOfInput() || s.indexOf(text.charAt(i())) == -1 ) - return false; - i(1); - return true; - } - - public boolean noneOf(String s) { - if( endOfInput() || s.indexOf(text.charAt(i())) != -1 ) - return false; - i(1); - return true; - } - - public boolean inCharRange(char cLow, char cHigh) { - if( endOfInput() ) - return false; - char c = text.charAt(i()); - if( !(cLow <= c && c <= cHigh) ) - return false; - i(1); - return true; - } - - public boolean anyChar() { - if( endOfInput() ) - return false; - i(1); - return true; - } - - public boolean test(char c) { - return !endOfInput() && text.charAt(i()) == c; - } - - public boolean test(String s) { - return text.regionMatches(i(),s,0,s.length()); - } - - public boolean testIgnoreCase(String s) { - return text.regionMatches(true,i(),s,0,s.length()); - } - - public String textFrom(int start) { - return text.substring(start,i()); - } - -}
diff -r 38a42f437fd2 -r 88b5b81cad4a src/luan/modules/parsers/Theme.java --- a/src/luan/modules/parsers/Theme.java Wed Aug 02 13:45:06 2017 -0600 +++ b/src/luan/modules/parsers/Theme.java Wed Aug 02 15:19:47 2017 -0600 @@ -1,6 +1,8 @@ package luan.modules.parsers; import luan.LuanException; +import luan.lib.parser.Parser; +import luan.lib.parser.ParseException; public final class Theme {