Skip to content

Commit

Permalink
Fix some assembly parse errors
Browse files Browse the repository at this point in the history
Closes #159
  • Loading branch information
trask committed Mar 31, 2015
1 parent 7d028fb commit 130802f
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 34 deletions.
Expand Up @@ -50,7 +50,7 @@ public AssemblyProcessor(PackageManager packageManager)


public void handleLine(final String inLine) public void handleLine(final String inLine)
{ {
String line = inLine.trim(); String line = inLine.replaceFirst("^ +", "");


line = line.replace(S_ENTITY_LT, S_OPEN_ANGLE); line = line.replace(S_ENTITY_LT, S_OPEN_ANGLE);
line = line.replace(S_ENTITY_GT, S_CLOSE_ANGLE); line = line.replace(S_ENTITY_GT, S_CLOSE_ANGLE);
Expand Down
Expand Up @@ -37,6 +37,11 @@ public void addInstruction(AssemblyInstruction instr)
instructions.add(instr); instructions.add(instr);
} }


public void replaceLastInstruction(AssemblyInstruction instr)
{
instructions.set(instructions.size() - 1, instr);
}

public List<AssemblyInstruction> getInstructions() public List<AssemblyInstruction> getInstructions()
{ {
return instructions; return instructions;
Expand Down
Expand Up @@ -105,7 +105,16 @@ public void addCommentLine(String comment)
{ {
if (comment != null) if (comment != null)
{ {
commentLines.add(comment.trim()); commentLines.add(comment);
}
}

public void appendToLastCommentLine(String comment)
{
if (comment != null)
{
String lastCommentLine = commentLines.get(commentLines.size() - 1);
commentLines.set(commentLines.size() - 1, lastCommentLine + comment);
} }
} }


Expand Down
Expand Up @@ -40,61 +40,119 @@ public static AssemblyMethod parseAssembly(final String asm)


AssemblyInstruction lastInstruction = null; AssemblyInstruction lastInstruction = null;


for (String line : lines) String lastLine = null;
for (int i = 0; i < lines.length; i++)
{ {
if (DEBUG_LOGGING_ASSEMBLY) if (DEBUG_LOGGING_ASSEMBLY)
{ {
logger.debug("line: '{}'", line); logger.debug("line: '{}'", lines[i]);
} }


String trimmedLine = line.replace(S_ENTITY_APOS, S_QUOTE).trim(); String line = lines[i].replace(S_ENTITY_APOS, S_QUOTE);
line = line.replaceFirst("^ +", "");


if (trimmedLine.startsWith(S_HASH)) if (line.startsWith(S_HASH))
{ {
if (DEBUG_LOGGING_ASSEMBLY) if (DEBUG_LOGGING_ASSEMBLY)
{ {
logger.debug("Assembly header: {}", trimmedLine); logger.debug("Assembly header: {}", line);
} }


headerBuilder.append(trimmedLine).append(S_NEWLINE); headerBuilder.append(line).append(S_NEWLINE);
} }
else if (trimmedLine.startsWith(S_OPEN_SQUARE)) else if (line.startsWith(S_OPEN_SQUARE))
{ {
if (DEBUG_LOGGING_ASSEMBLY) if (DEBUG_LOGGING_ASSEMBLY)
{ {
logger.debug("new AssemblyBlock: {}", trimmedLine); logger.debug("new AssemblyBlock: {}", line);
} }


method.addBlock(currentBlock); method.addBlock(currentBlock);
currentBlock = new AssemblyBlock(); currentBlock = new AssemblyBlock();
currentBlock.setTitle(trimmedLine); currentBlock.setTitle(line);
} }
else if (trimmedLine.startsWith(S_SEMICOLON)) else if (line.startsWith(S_SEMICOLON))
{ {
if (DEBUG_LOGGING_ASSEMBLY) if (DEBUG_LOGGING_ASSEMBLY)
{ {
logger.debug("Extended comment? '{}'", trimmedLine); logger.debug("Extended comment? '{}'", line);
} }


if (lastInstruction != null) if (lastInstruction != null)
{ {
if (trimmedLine.length() > 0) lastInstruction.addCommentLine(line);
{
lastInstruction.addCommentLine(trimmedLine);
}
} }
} }
else else
{ {
AssemblyInstruction instr = createInstruction(labels, trimmedLine); AssemblyInstruction instr = createInstruction(labels, line);


if (instr != null) if (instr == null
&& lastLine.trim().startsWith(S_HASH)
&& !line.startsWith(S_ASSEMBLY_ADDRESS)
&& !line.contains(' ' + S_ASSEMBLY_ADDRESS))
{ {
currentBlock.addInstruction(instr); // remove last newline

headerBuilder.setLength(headerBuilder.length() - S_NEWLINE.length());
lastInstruction = instr;
headerBuilder.append(line).append(S_NEWLINE);

// update untrimmedLine since it is used to update lastUntrimmedLine at end of loop
line = lastLine + line;
}
else if (instr == null
&& lastLine.trim().startsWith(S_SEMICOLON)
&& lastInstruction != null)
{
lastInstruction.appendToLastCommentLine(line);

// update untrimmedLine since it is used to update lastUntrimmedLine at end of loop
line = lastLine + line;
}
else
{
boolean replaceLast = false;
if (instr == null && i < lines.length - 1)
{
// try appending current and next lines together
String nextUntrimmedLine = lines[i + 1].replace(S_ENTITY_APOS, S_QUOTE);

instr = createInstruction(labels, line + nextUntrimmedLine);
if (instr != null) {
i++;
}
}

if (instr == null && lastInstruction != null)
{
// try appending last and current lines together
instr = createInstruction(labels, lastLine + line);
if (instr != null)
{
replaceLast = true;
}
}

if (instr != null)
{
if (replaceLast)
{
currentBlock.replaceLastInstruction(instr);
}
else
{
currentBlock.addInstruction(instr);
}

lastInstruction = instr;
}
else
{
logger.error("Could not parse assembly: {}", line);
}
} }
} }
lastLine = line;
} }


method.addBlock(currentBlock); method.addBlock(currentBlock);
Expand All @@ -120,12 +178,15 @@ public static AssemblyInstruction createInstruction(


String annotation = S_EMPTY; String annotation = S_EMPTY;


int addressIndex = line.indexOf(S_ASSEMBLY_ADDRESS); if (!line.startsWith(S_ASSEMBLY_ADDRESS))

if (addressIndex != -1)
{ {
annotation = line.substring(0, addressIndex); int addressIndex = line.indexOf(' ' + S_ASSEMBLY_ADDRESS);
line = line.substring(addressIndex);
if (addressIndex != -1)
{
annotation = line.substring(0, addressIndex) + ' ';
line = line.substring(addressIndex + 1);
}
} }


Matcher matcher = PATTERN_ASSEMBLY_INSTRUCTION.matcher(line); Matcher matcher = PATTERN_ASSEMBLY_INSTRUCTION.matcher(line);
Expand Down Expand Up @@ -187,10 +248,6 @@ else if (midParts.length >= 3)
labels.newInstruction(instr); labels.newInstruction(instr);
} }
} }
else
{
logger.error("Could not parse assembly: {}", line);
}


return instr; return instr;
} }
Expand Down

0 comments on commit 130802f

Please sign in to comment.