Commit b2134fa4 authored by Enteocline's avatar Enteocline

Le parser est relié à l'interface graphique.

parent 0fb946aa
......@@ -7,6 +7,8 @@ import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
/** First iteration of the panel for ingame programming
Created by Alexis Ghyselen **/
......@@ -49,7 +51,7 @@ public class IGPpanel extends graphics.guiSkeleton.guiPanel.GUIPanel{
add(titlePanel, c);
/** Creation of the buttonPanel **/
JPanel buttonPanel = new JPanel();
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridBagLayout());
buttonPanel.setOpaque(true);
buttonPanel.setBackground(GUIColorsAndFonts.backGround1);
......@@ -89,6 +91,7 @@ public class IGPpanel extends graphics.guiSkeleton.guiPanel.GUIPanel{
prflabel[i].setText("");
prflabel[i].setForeground(GUIColorsAndFonts.fontColor1);
prflabel[i].setBackground(GUIColorsAndFonts.buttonBackground1);
prflabel[i].addActionListener(parselistener);
c2 = new GridBagConstraints();
c2.gridx=3; c2.gridy=(i+1); c2.gridwidth=2; c2.gridheight=1; c2.weightx=3; c2.weighty=1; c2.fill=GridBagConstraints.BOTH;
buttonPanel.add(prflabel[i],c2);
......@@ -99,9 +102,9 @@ public class IGPpanel extends graphics.guiSkeleton.guiPanel.GUIPanel{
c2 = new GridBagConstraints();
c2.gridx=5; c2.gridy=(i+1); c2.gridwidth=2; c2.gridheight=1; c2.weightx=3; c2.weighty=1; c2.fill=GridBagConstraints.BOTH;
buttonPanel.add(hintlabel[i],c2);
}
/** Back button **/
JButton exitButton = new JButton();
exitButton.setFont(GUIColorsAndFonts.buttonFont1);
......@@ -154,7 +157,7 @@ public class IGPpanel extends graphics.guiSkeleton.guiPanel.GUIPanel{
hintlabel[vswitch].setText(s);
bswitch[vswitch].setBackground(GUIColorsAndFonts.buttonBackground1);
vswitch = -1;
}
}
};
......@@ -166,8 +169,34 @@ public class IGPpanel extends graphics.guiSkeleton.guiPanel.GUIPanel{
}
};
/**
* ActionListener to parse an instruction.
* Should be modified soon.
*/
private ActionListener parselistener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Script parser = new Script(stringToStream(((JTextField)e.getSource()).getText()));
if (parser.checkInput()) {
System.out.println("L'instruction est correcte");
} else {
System.out.println("Erreur dans l'instruction");
}
}
};
/**
* Convert a String to an equivalent InputStream.
* @param the String to convert.
* @return the equivalent InputStream.
*/
private InputStream stringToStream(String s) {
InputStream is = new ByteArrayInputStream(s.getBytes());
return is;
}
@Override
public void initialise() {
......@@ -187,11 +216,10 @@ public class IGPpanel extends graphics.guiSkeleton.guiPanel.GUIPanel{
public void keyReleasedHandler(Input e) {
}
@Override
public void keyTypedHandler(Input e) {
}
}
}
......@@ -3,14 +3,21 @@ package ingame_programming;
/** Interpréteur du langage de script */
public class Script implements ScriptConstants {
/* TODO. Pour l'instant, on analyse les phrases depuis la ligne de commande. */
public static void main(String args[]) throws ParseException {
Script parser = new Script(System.in);
parser.Instruction();
/**
* Check whether the input is correct.
* @return a boolean about the correctness of the input string.
*/
public boolean checkInput() {
try {
Instruction();
return true;
} catch(ParseException e) {
return false;
}
}
static final public void Instruction() throws ParseException {
final public void Instruction() throws ParseException {
String s, ss;
Token t;
int count = 0;
......@@ -66,7 +73,7 @@ public class Script implements ScriptConstants {
}
/** Détail des options. */
static final public String Option() throws ParseException {
final public String Option() throws ParseException {
String s, ss;
Token t;
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
......@@ -190,17 +197,16 @@ public class Script implements ScriptConstants {
throw new Error("Missing return statement in function");
}
static private boolean jj_initialized_once = false;
/** Generated Token Manager. */
static public ScriptTokenManager token_source;
static SimpleCharStream jj_input_stream;
public ScriptTokenManager token_source;
SimpleCharStream jj_input_stream;
/** Current token. */
static public Token token;
public Token token;
/** Next token. */
static public Token jj_nt;
static private int jj_ntk;
static private int jj_gen;
static final private int[] jj_la1 = new int[6];
public Token jj_nt;
private int jj_ntk;
private int jj_gen;
final private int[] jj_la1 = new int[6];
static private int[] jj_la1_0;
static {
jj_la1_init_0();
......@@ -215,13 +221,6 @@ public class Script implements ScriptConstants {
}
/** Constructor with InputStream and supplied encoding */
public Script(java.io.InputStream stream, String encoding) {
if (jj_initialized_once) {
System.out.println("ERROR: Second call to constructor of static parser. ");
System.out.println(" You must either use ReInit() or set the JavaCC option STATIC to false");
System.out.println(" during parser generation.");
throw new Error();
}
jj_initialized_once = true;
try { jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
token_source = new ScriptTokenManager(jj_input_stream);
token = new Token();
......@@ -231,11 +230,11 @@ public class Script implements ScriptConstants {
}
/** Reinitialise. */
static public void ReInit(java.io.InputStream stream) {
public void ReInit(java.io.InputStream stream) {
ReInit(stream, null);
}
/** Reinitialise. */
static public void ReInit(java.io.InputStream stream, String encoding) {
public void ReInit(java.io.InputStream stream, String encoding) {
try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
token_source.ReInit(jj_input_stream);
token = new Token();
......@@ -246,13 +245,6 @@ public class Script implements ScriptConstants {
/** Constructor. */
public Script(java.io.Reader stream) {
if (jj_initialized_once) {
System.out.println("ERROR: Second call to constructor of static parser. ");
System.out.println(" You must either use ReInit() or set the JavaCC option STATIC to false");
System.out.println(" during parser generation.");
throw new Error();
}
jj_initialized_once = true;
jj_input_stream = new SimpleCharStream(stream, 1, 1);
token_source = new ScriptTokenManager(jj_input_stream);
token = new Token();
......@@ -262,7 +254,7 @@ public class Script implements ScriptConstants {
}
/** Reinitialise. */
static public void ReInit(java.io.Reader stream) {
public void ReInit(java.io.Reader stream) {
jj_input_stream.ReInit(stream, 1, 1);
token_source.ReInit(jj_input_stream);
token = new Token();
......@@ -273,13 +265,6 @@ public class Script implements ScriptConstants {
/** Constructor with generated Token Manager. */
public Script(ScriptTokenManager tm) {
if (jj_initialized_once) {
System.out.println("ERROR: Second call to constructor of static parser. ");
System.out.println(" You must either use ReInit() or set the JavaCC option STATIC to false");
System.out.println(" during parser generation.");
throw new Error();
}
jj_initialized_once = true;
token_source = tm;
token = new Token();
jj_ntk = -1;
......@@ -296,7 +281,7 @@ public class Script implements ScriptConstants {
for (int i = 0; i < 6; i++) jj_la1[i] = -1;
}
static private Token jj_consume_token(int kind) throws ParseException {
private Token jj_consume_token(int kind) throws ParseException {
Token oldToken;
if ((oldToken = token).next != null) token = token.next;
else token = token.next = token_source.getNextToken();
......@@ -312,7 +297,7 @@ public class Script implements ScriptConstants {
/** Get the next Token. */
static final public Token getNextToken() {
final public Token getNextToken() {
if (token.next != null) token = token.next;
else token = token.next = token_source.getNextToken();
jj_ntk = -1;
......@@ -321,7 +306,7 @@ public class Script implements ScriptConstants {
}
/** Get the specific Token. */
static final public Token getToken(int index) {
final public Token getToken(int index) {
Token t = token;
for (int i = 0; i < index; i++) {
if (t.next != null) t = t.next;
......@@ -330,19 +315,19 @@ public class Script implements ScriptConstants {
return t;
}
static private int jj_ntk() {
private int jj_ntk() {
if ((jj_nt=token.next) == null)
return (jj_ntk = (token.next=token_source.getNextToken()).kind);
else
return (jj_ntk = jj_nt.kind);
}
static private java.util.List<int[]> jj_expentries = new java.util.ArrayList<int[]>();
static private int[] jj_expentry;
static private int jj_kind = -1;
private java.util.List<int[]> jj_expentries = new java.util.ArrayList<int[]>();
private int[] jj_expentry;
private int jj_kind = -1;
/** Generate ParseException. */
static public ParseException generateParseException() {
public ParseException generateParseException() {
jj_expentries.clear();
boolean[] la1tokens = new boolean[20];
if (jj_kind >= 0) {
......@@ -373,11 +358,11 @@ public class Script implements ScriptConstants {
}
/** Enable tracing. */
static final public void enable_tracing() {
final public void enable_tracing() {
}
/** Disable tracing. */
static final public void disable_tracing() {
final public void disable_tracing() {
}
}
......@@ -5,17 +5,28 @@
*
*/
options {
STATIC = false;
}
PARSER_BEGIN(Script)
package ingame_programming;
/** Interpréteur du langage de script */
public class Script {
/* TODO. Pour l'instant, on analyse les phrases depuis la ligne de commande. */
public static void main(String args[]) throws ParseException {
Script parser = new Script(System.in);
parser.Instruction();
/**
* Check whether the input is correct.
* @return a boolean about the correctness of the input string.
*/
public boolean checkInput() {
try {
Instruction();
return true;
} catch(ParseException e) {
return false;
}
}
}
......
......@@ -6,10 +6,10 @@ public class ScriptTokenManager implements ScriptConstants
{
/** Debug output. */
public static java.io.PrintStream debugStream = System.out;
public java.io.PrintStream debugStream = System.out;
/** Set debug output. */
public static void setDebugStream(java.io.PrintStream ds) { debugStream = ds; }
private static final int jjStopStringLiteralDfa_0(int pos, long active0)
public void setDebugStream(java.io.PrintStream ds) { debugStream = ds; }
private final int jjStopStringLiteralDfa_0(int pos, long active0)
{
switch (pos)
{
......@@ -41,17 +41,17 @@ private static final int jjStopStringLiteralDfa_0(int pos, long active0)
return -1;
}
}
private static final int jjStartNfa_0(int pos, long active0)
private final int jjStartNfa_0(int pos, long active0)
{
return jjMoveNfa_0(jjStopStringLiteralDfa_0(pos, active0), pos + 1);
}
static private int jjStopAtPos(int pos, int kind)
private int jjStopAtPos(int pos, int kind)
{
jjmatchedKind = kind;
jjmatchedPos = pos;
return pos + 1;
}
static private int jjMoveStringLiteralDfa0_0()
private int jjMoveStringLiteralDfa0_0()
{
switch(curChar)
{
......@@ -67,7 +67,7 @@ static private int jjMoveStringLiteralDfa0_0()
return jjMoveNfa_0(6, 0);
}
}
static private int jjMoveStringLiteralDfa1_0(long active0)
private int jjMoveStringLiteralDfa1_0(long active0)
{
try { curChar = input_stream.readChar(); }
catch(java.io.IOException e) {
......@@ -85,7 +85,7 @@ static private int jjMoveStringLiteralDfa1_0(long active0)
}
return jjStartNfa_0(0, active0);
}
static private int jjMoveStringLiteralDfa2_0(long old0, long active0)
private int jjMoveStringLiteralDfa2_0(long old0, long active0)
{
if (((active0 &= old0)) == 0L)
return jjStartNfa_0(0, old0);
......@@ -107,7 +107,7 @@ static private int jjMoveStringLiteralDfa2_0(long old0, long active0)
}
return jjStartNfa_0(1, active0);
}
static private int jjMoveStringLiteralDfa3_0(long old0, long active0)
private int jjMoveStringLiteralDfa3_0(long old0, long active0)
{
if (((active0 &= old0)) == 0L)
return jjStartNfa_0(1, old0);
......@@ -127,7 +127,7 @@ static private int jjMoveStringLiteralDfa3_0(long old0, long active0)
}
return jjStartNfa_0(2, active0);
}
static private int jjStartNfaWithStates_0(int pos, int kind, int state)
private int jjStartNfaWithStates_0(int pos, int kind, int state)
{
jjmatchedKind = kind;
jjmatchedPos = pos;
......@@ -147,7 +147,7 @@ static final long[] jjbitVec2 = {
static final long[] jjbitVec3 = {
0x0L, 0x0L, 0x0L, 0x30000000000L
};
static private int jjMoveNfa_0(int startState, int curPos)
private int jjMoveNfa_0(int startState, int curPos)
{
int startsAt = 0;
jjnewStateCnt = 111;
......@@ -785,14 +785,14 @@ static final long[] jjtoToken = {
static final long[] jjtoSkip = {
0xeL,
};
static protected SimpleCharStream input_stream;
static private final int[] jjrounds = new int[111];
static private final int[] jjstateSet = new int[222];
static protected char curChar;
protected SimpleCharStream input_stream;
private final int[] jjrounds = new int[111];
private final int[] jjstateSet = new int[222];
protected char curChar;
/** Constructor. */
public ScriptTokenManager(SimpleCharStream stream){
if (input_stream != null)
throw new TokenMgrError("ERROR: Second call to constructor of static lexer. You must use ReInit() to initialize the static variables.", TokenMgrError.STATIC_LEXER_ERROR);
if (SimpleCharStream.staticFlag)
throw new Error("ERROR: Cannot use a static CharStream class with a non-static lexical analyzer.");
input_stream = stream;
}
......@@ -803,14 +803,14 @@ public ScriptTokenManager(SimpleCharStream stream, int lexState){
}
/** Reinitialise parser. */
static public void ReInit(SimpleCharStream stream)
public void ReInit(SimpleCharStream stream)
{
jjmatchedPos = jjnewStateCnt = 0;
curLexState = defaultLexState;
input_stream = stream;
ReInitRounds();
}
static private void ReInitRounds()
private void ReInitRounds()
{
int i;
jjround = 0x80000001;
......@@ -819,14 +819,14 @@ static private void ReInitRounds()
}
/** Reinitialise parser. */
static public void ReInit(SimpleCharStream stream, int lexState)
public void ReInit(SimpleCharStream stream, int lexState)
{
ReInit(stream);
SwitchTo(lexState);
}
/** Switch to specified lex state. */
static public void SwitchTo(int lexState)
public void SwitchTo(int lexState)
{
if (lexState >= 1 || lexState < 0)
throw new TokenMgrError("Error: Ignoring invalid lexical state : " + lexState + ". State unchanged.", TokenMgrError.INVALID_LEXICAL_STATE);
......@@ -834,7 +834,7 @@ static public void SwitchTo(int lexState)
curLexState = lexState;
}
static protected Token jjFillToken()
protected Token jjFillToken()
{
final Token t;
final String curTokenImage;
......@@ -858,15 +858,15 @@ static protected Token jjFillToken()
return t;
}
static int curLexState = 0;
static int defaultLexState = 0;
static int jjnewStateCnt;
static int jjround;
static int jjmatchedPos;
static int jjmatchedKind;
int curLexState = 0;
int defaultLexState = 0;
int jjnewStateCnt;
int jjround;
int jjmatchedPos;
int jjmatchedKind;
/** Get the next Token. */
public static Token getNextToken()
public Token getNextToken()
{
Token matchedToken;
int curPos = 0;
......@@ -930,7 +930,7 @@ public static Token getNextToken()
}
}
static private void jjCheckNAdd(int state)
private void jjCheckNAdd(int state)
{
if (jjrounds[state] != jjround)
{
......@@ -938,13 +938,13 @@ static private void jjCheckNAdd(int state)
jjrounds[state] = jjround;
}
}
static private void jjAddStates(int start, int end)
private void jjAddStates(int start, int end)
{
do {
jjstateSet[jjnewStateCnt++] = jjnextStates[start];
} while (start++ != end);
}
static private void jjCheckNAddTwoStates(int state1, int state2)
private void jjCheckNAddTwoStates(int state1, int state2)
{
jjCheckNAdd(state1);
jjCheckNAdd(state2);
......
/* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 5.0 */
/* JavaCCOptions:STATIC=true,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
/* JavaCCOptions:STATIC=false,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
package ingame_programming;
/**
......@@ -10,33 +10,33 @@ package ingame_programming;
public class SimpleCharStream
{
/** Whether parser is static. */
public static final boolean staticFlag = true;
static int bufsize;
static int available;
static int tokenBegin;
public static final boolean staticFlag = false;
int bufsize;
int available;
int tokenBegin;
/** Position in buffer. */
static public int bufpos = -1;
static protected int bufline[];
static protected int bufcolumn[];
public int bufpos = -1;
protected int bufline[];
protected int bufcolumn[];
static protected int column = 0;
static protected int line = 1;
protected int column = 0;
protected int line = 1;
static protected boolean prevCharIsCR = false;
static protected boolean prevCharIsLF = false;
protected boolean prevCharIsCR = false;
protected boolean prevCharIsLF = false;
static protected java.io.Reader inputStream;
protected java.io.Reader inputStream;
static protected char[] buffer;
static protected int maxNextCharInd = 0;
static protected int inBuf = 0;
static protected int tabSize = 8;
protected char[] buffer;
protected int maxNextCharInd = 0;
protected int inBuf = 0;
protected int tabSize = 8;
static protected void setTabSize(int i) { tabSize = i; }
static protected int getTabSize(int i) { return tabSize; }
protected void setTabSize(int i) { tabSize = i; }
protected int getTabSize(int i) { return tabSize; }
static protected void ExpandBuff(boolean wrapAround)
protected void ExpandBuff(boolean wrapAround)
{
char[] newbuffer = new char[bufsize + 2048];
int newbufline[] = new int[bufsize + 2048];
......@@ -85,7 +85,7 @@ public class SimpleCharStream
tokenBegin = 0;
}
static protected void FillBuff() throws java.io.IOException
protected void FillBuff() throws java.io.IOException
{
if (maxNextCharInd == available)
{
......@@ -130,7 +130,7 @@ public class SimpleCharStream
}
/** Start. */
static public char BeginToken() throws java.io.IOException
public char BeginToken() throws java.io.IOException
{
tokenBegin = -1;
char c = readChar();
......@@ -139,7 +139,7 @@ public class SimpleCharStream
return c;
}
static protected void UpdateLineColumn(char c)
protected void UpdateLineColumn(char c)
{
column++;
......@@ -180,7 +180,7 @@ public class SimpleCharStream
}
/** Read a character. */
static public char readChar() throws java.io.IOException
public char readChar() throws java.io.IOException
{
if (inBuf > 0)
{
......@@ -207,7 +207,7 @@ public class SimpleCharStream
* @see #getEndColumn
*/
static public int getColumn() {
public int getColumn() {
return bufcolumn[bufpos];
}
......@@ -217,32 +217,32 @@ public class SimpleCharStream
* @see #getEndLine
*/
static public int getLine() {
public int getLine() {
return bufline[bufpos];
}
/** Get token end column number. */
static public int getEndColumn() {
public int getEndColumn() {
return bufcolumn[bufpos];
}
/** Get token end line number. */
static public int getEndLine() {
public int getEndLine() {
return bufline[bufpos];
}
/** Get token beginning column number. */
static public int getBeginColumn() {
public int getBeginColumn() {
return bufcolumn[tokenBegin];
}
/** Get token beginning line number. */
static public int getBeginLine() {
public int getBeginLine() {
return bufline[tokenBegin];
}
/** Backup a number of characters. */
static public void backup(int amount) {
public void backup(int amount) {
inBuf += amount;
if ((bufpos -= amount) < 0)
......@@ -253,10 +253,6 @@ public class SimpleCharStream
public SimpleCharStream(java.io.Reader dstream, int startline,
int startcolumn, int buffersize)