Mercurial Hosting > luan
changeset 165:94bbc4cbc106
merge luan/parser into interp
git-svn-id: https://luan-java.googlecode.com/svn/trunk@166 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Sun, 22 Jun 2014 04:10:59 +0000 |
parents | 78ba371ea1e9 |
children | 4eaee12f6c65 |
files | src/luan/interp/LuanCompiler.java src/luan/interp/LuanParser.java src/luan/interp/ParseException.java src/luan/interp/Parser.java src/luan/parser/ParseException.java src/luan/parser/Parser.java |
diffstat | 6 files changed, 207 insertions(+), 210 deletions(-) [+] |
line wrap: on
line diff
--- a/src/luan/interp/LuanCompiler.java Fri Jun 20 23:23:16 2014 +0000 +++ b/src/luan/interp/LuanCompiler.java Sun Jun 22 04:10:59 2014 +0000 @@ -6,7 +6,6 @@ import luan.LuanSource; import luan.LuanElement; import luan.LuanTable; -import luan.parser.ParseException; import java.util.Map;
--- a/src/luan/interp/LuanParser.java Fri Jun 20 23:23:16 2014 +0000 +++ b/src/luan/interp/LuanParser.java Sun Jun 22 04:10:59 2014 +0000 @@ -9,8 +9,6 @@ import luan.Luan; import luan.LuanState; import luan.LuanSource; -import luan.parser.Parser; -import luan.parser.ParseException; final class LuanParser {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/interp/ParseException.java Sun Jun 22 04:10:59 2014 +0000 @@ -0,0 +1,55 @@ +package luan.interp; + +import luan.LuanSource; + + +public final class ParseException extends Exception { + public final LuanSource src; + public final int iCurrent; + public final int iHigh; + + ParseException(String msg,LuanSource src,int iCurrent,int iHigh) { + super(msg); + this.src = src; + this.iCurrent = iCurrent; + this.iHigh = iHigh; +//System.out.println("iCurrent = "+iCurrent); +//System.out.println("iHigh = "+iHigh); + } + + private class Location { + final int line; + final int pos; + + Location(int index) { + int line = 0; + int i = -1; + while(true) { + int j = src.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 src.text.split("\n",-1); + } + + public String getFancyMessage() { + Location loc = new Location(iCurrent); + String line = lines()[loc.line]; + String msg = getMessage() + " (line " + (loc.line+1) + ", pos " + (loc.pos+1) + ") in " + src.name + "\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(); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/interp/Parser.java Sun Jun 22 04:10:59 2014 +0000 @@ -0,0 +1,152 @@ +package luan.interp; + +import luan.LuanSource; + + +final class Parser { + private final LuanSource src; + public final String text; + private final int len; + private int[] stack = new int[256]; + private int frame = 0; + private int iHigh; + + public Parser(LuanSource src) { + this.src = src; + this.text = src.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 ParseException exception(String msg) { + return new ParseException(msg,src,i(),iHigh); + } + + public ParseException exception() { + return exception("Invalid input"); + } + + public int currentIndex() { + return i(); + } + + 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(String s) { + return text.regionMatches(i(),s,0,s.length()); + } + + public String textFrom(int start) { + return text.substring(start,i()); + } + +}
--- a/src/luan/parser/ParseException.java Fri Jun 20 23:23:16 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,55 +0,0 @@ -package luan.parser; - -import luan.LuanSource; - - -public class ParseException extends Exception { - public final LuanSource src; - public final int iCurrent; - public final int iHigh; - - ParseException(String msg,LuanSource src,int iCurrent,int iHigh) { - super(msg); - this.src = src; - this.iCurrent = iCurrent; - this.iHigh = iHigh; -//System.out.println("iCurrent = "+iCurrent); -//System.out.println("iHigh = "+iHigh); - } - - private class Location { - final int line; - final int pos; - - Location(int index) { - int line = 0; - int i = -1; - while(true) { - int j = src.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 src.text.split("\n",-1); - } - - public String getFancyMessage() { - Location loc = new Location(iCurrent); - String line = lines()[loc.line]; - String msg = getMessage() + " (line " + (loc.line+1) + ", pos " + (loc.pos+1) + ") in " + src.name + "\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(); - } -}
--- a/src/luan/parser/Parser.java Fri Jun 20 23:23:16 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,152 +0,0 @@ -package luan.parser; - -import luan.LuanSource; - - -public final class Parser { - private final LuanSource src; - public final String text; - private final int len; - private int[] stack = new int[256]; - private int frame = 0; - private int iHigh; - - public Parser(LuanSource src) { - this.src = src; - this.text = src.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 ParseException exception(String msg) { - return new ParseException(msg,src,i(),iHigh); - } - - public ParseException exception() { - return exception("Invalid input"); - } - - public int currentIndex() { - return i(); - } - - 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(String s) { - return text.regionMatches(i(),s,0,s.length()); - } - - public String textFrom(int start) { - return text.substring(start,i()); - } - -}