Skip to content

Commit

Permalink
Command parser works ... in theory
Browse files Browse the repository at this point in the history
  • Loading branch information
ProjectMoon committed Jun 30, 2010
1 parent 497bdb0 commit 3fe0063
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 42 deletions.
51 changes: 12 additions & 39 deletions sample/data/test.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,12 @@
from ring.nrapi.xml import XMLConverter
from ring.mobiles import RaceFactory
from ring.mobiles.npc import NPC
from ring.movement import Room, Location, Portal

__document__.codebehind = "code/events.py"

mob = NPC()
mob.ID = "someGuy"
mob.baseModel.name = "Some guy"
mob.baseModel.race = RaceFactory.createHuman()
mob.baseModel.description = "You look at this guy and your mind EXPLODES WITH [RED][B]AWESOME![R]"

room1 = Room()
room1.ID = "room1"
room1.model.title = "In the Void"
room1.model.description = "You are somewhere in the void of the cosmos..."
room1.addMobile(mob)

room2 = Room()
room2.ID = "room2"
room2.model.title = "Somewhere else in the void"
room2.model.description = "You are in another place in the void of the cosmos..."

loc1 = XMLConverter.create(Location)
loc1.ID = "loc1"
loc1.room = room1
p1 = Portal()
p1.destination = room2
p1.displayName = "north"
loc1.addExit(p1)

loc2 = XMLConverter.create(Location)
loc2.ID = "loc2"
loc2.room = room2
p2 = Portal()
p2.destination = room1
p2.displayName = "south"
loc2.addExit(p2)
import jynx
from java.lang import Object
from jynx.lib.junit import*
from jynx.jannotations import*

@JavaClass
class JavaCompilerTest(Object):
@Override
def blah(self):
print "blah"

JavaCompilerTest()
15 changes: 14 additions & 1 deletion src/ring/commands/annotations/CommandForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,19 @@ private void parse(Form form) {

String[] split = form.clause().split(" ");
int c = 0;
int count = 0;

for (String tokenString : split) {
if (!tokenString.equals("")) {
CommandToken token = new CommandToken();

token.setToken(tokenString);

//The parser needs to keep track of if this is at the start.
if (count == 0) {
token.setAtStart(true);
}

//Determine if is variable or delimiter and handle accordingly.
if (tokenString.startsWith(":") || tokenString.startsWith("$")) {
token.setVariable(true);
Class<?>[] types = form.bind()[c].value();
Expand All @@ -137,8 +144,14 @@ private void parse(Form form) {
}

tokens.add(token);
count++;
}
}

//Set atEnd property for the last command token.
if (tokens.size() > 0) {
tokens.get(tokens.size() - 1).setAtEnd(true);
}
}

public String toString() {
Expand Down
15 changes: 13 additions & 2 deletions src/ring/commands/annotations/CommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@Template({
@Form(bind = { @BindType({String.class}) }),
@Form(id = "lookThing", clause = ":thing", bind = { @BindType({String.class, Class.class}) }),
@Form(id = "lookAway", clause = "away :thing", bind = { @BindType({String.class, Class.class}) }),
@Form(id = "lookAway", clause = ":thing away", bind = { @BindType({String.class, Class.class}) }),
@Form(id = "lookAt", clause = "at :thing in $box", bind = { @BindType({String.class}), @BindType({Class.class}) }),
})
public class CommandParser {
Expand Down Expand Up @@ -86,7 +86,15 @@ private List<ParsedCommandToken> testForm(CommandForm form, String clause) {

boolean found = false;
for (; c < split.length; c++) {
if (delim.isAtStart() && delim.isDelimiter() && c == 0) {
if (!delim.getToken().equals(split[c])) {
errors = true;
break;
}
}

if (!split[c].equals(currDelim)) {
//Make sure we don't slip up on delims and arguments that get mixed.
if (prevDelim != null && split[c].equals(prevDelim)) {
prevToken.setEndIndex(c);
currToken.setStartIndex(c + 1);
Expand All @@ -101,7 +109,10 @@ private List<ParsedCommandToken> testForm(CommandForm form, String clause) {

if (!found) {
errors = true;
break;
}

if (errors) {
break;
}

prevDelim = currDelim;
Expand Down
20 changes: 20 additions & 0 deletions src/ring/commands/annotations/CommandToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
public class CommandToken {
private boolean isDelimiter;
private boolean isVariable;
private boolean isAtStart;
private boolean isAtEnd;

private String token;

Expand Down Expand Up @@ -56,6 +58,24 @@ public void setBindTypes(List<Class<?>> bindTypes) {
}
}

public boolean isAtStart() {
return isAtStart;
}

public void setAtStart(boolean beginning) {
isAtStart = beginning;
isAtEnd = !isAtStart;
}

public boolean isAtEnd() {
return isAtEnd;
}

public void setAtEnd(boolean end) {
isAtEnd = end;
isAtStart = !isAtEnd;
}

public String toString() {
return getToken();
}
Expand Down

0 comments on commit 3fe0063

Please sign in to comment.