Skip to content

Commit

Permalink
fix(formatter): many formatter fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
prb28 committed Nov 13, 2018
1 parent 2099689 commit 029e379
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 17 deletions.
55 changes: 41 additions & 14 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,41 @@ export class ASMLine {
this.commentRange = new Range(new Position(lineNumber, commentPosInInputLine), new Position(lineNumber, commentPosInInputLine + this.comment.length));
}
// find a keywork
let keyword = this.search(ASMLine.keywordsRegExps, l);
if (!keyword && leadingSpacesCount !== 0) {
// it's not a keyword - this could be a macro iif there are leading spaces
// Consider it is a label iif there are no leading spaces
if (leadingSpacesCount !== 0) {
keyword = this.search(ASMLine.macrosRegExps, l);
// remove quotes
let searchInstructionString = l;
let keywordIndex = 0;
if (leadingSpacesCount === 0) {
// Fist word must be a label
let sPos = line.search(/\s/);
if (sPos > 0) {
searchInstructionString = searchInstructionString.substring(sPos);
keywordIndex = sPos;
}
}
let qPos = searchInstructionString.indexOf("\"");
if (qPos > 0) {
searchInstructionString = searchInstructionString.substring(0, qPos);
}
qPos = searchInstructionString.indexOf("'");
if (qPos > 0) {
searchInstructionString = searchInstructionString.substring(0, qPos);
}
let keyword = this.search(ASMLine.keywordsRegExps, searchInstructionString);
if (!keyword && leadingSpacesCount !== 0) {
// it's not a keyword - this could be a macro if there are leading spaces
// Consider it is a label if there are no leading spaces
keyword = this.search(ASMLine.macrosRegExps, searchInstructionString);
}
if (keyword) {
// A keyword has been found
// set the keyword
this.instruction = keyword[0];
let startInInputLine = leadingSpacesCount + keyword.index;
keywordIndex += keyword.index;
let startInInputLine = leadingSpacesCount + keywordIndex;
let endInInputLine = startInInputLine + this.instruction.length;
this.instructionRange = new Range(new Position(lineNumber, startInInputLine), new Position(lineNumber, endInInputLine));
if (keyword.index > 0) {
this.label = l.substring(0, keyword.index).trim();
if (keywordIndex > 0) {
this.label = l.substring(0, keywordIndex).trim();
next = new Position(lineNumber, leadingSpacesCount + this.label.length);
this.labelRange = new Range(current, next);
current = next;
Expand All @@ -107,10 +125,11 @@ export class ASMLine {
current = next;
}
current = this.instructionRange.end;
let endInTrimLine = keyword.index + keyword[0].length;
this.data = l.substring(endInTrimLine).trim();
let endInTrimLine = keywordIndex + keyword[0].length;
let dataStr = l.substring(endInTrimLine);
this.data = dataStr.trim();
if (this.data.length > 0) {
startInInputLine = line.indexOf(this.data);
startInInputLine = this.instructionRange.end.character + dataStr.indexOf(this.data);
next = new Position(lineNumber, startInInputLine);
this.spacesInstructionToDataRange = new Range(current, next);
current = next;
Expand Down Expand Up @@ -152,13 +171,21 @@ export class ASMLine {
* @return RegExpExecArray if found or null
*/
search(regexps: Array<RegExp>, value: string): any {
let firstMatch: any | null = null;
for (let regexp of regexps) {
let r = regexp.exec(value);
if (r) {
return r;
if (firstMatch !== null) {
// Which one is the first in the line
if (r.index < firstMatch.index) {
firstMatch = r;
}
} else {
firstMatch = r;
}
}
}
return null;
return firstMatch;
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/test/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,25 @@ describe("Parser Tests", function () {
expect(asmLine.commentRange).to.be.eql(new Range(new Position(0, 35), new Position(0, 46)));
asmLine = new ASMLine("mylabel");
expect(asmLine.labelRange).to.be.eql(new Range(new Position(0, 0), new Position(0, 7)));
asmLine = new ASMLine("Pdl0Wait dc.l\t0 \t;# of cycles until paddle timer is done");
expect(asmLine.labelRange).to.be.eql(new Range(new Position(0, 0), new Position(0, 8)));
expect(asmLine.instructionRange).to.be.eql(new Range(new Position(0, 9), new Position(0, 13)));
expect(asmLine.dataRange).to.be.eql(new Range(new Position(0, 14), new Position(0, 15)));
expect(asmLine.commentRange).to.be.eql(new Range(new Position(0, 17), new Position(0, 56)));
asmLine = new ASMLine("\t\taddq.l #6,Cycles");
expect(asmLine.instructionRange).to.be.eql(new Range(new Position(0, 2), new Position(0, 8)));
expect(asmLine.dataRange).to.be.eql(new Range(new Position(0, 9), new Position(0, 18)));
asmLine = new ASMLine(".Save12Msg\tdc.b \"Save disk from\",10,\"drive 1 or 2 ? \",0");
expect(asmLine.labelRange).to.be.eql(new Range(new Position(0, 0), new Position(0, 10)));
expect(asmLine.instructionRange).to.be.eql(new Range(new Position(0, 11), new Position(0, 15)));
expect(asmLine.dataRange).to.be.eql(new Range(new Position(0, 16), new Position(0, 66)));
asmLine = new ASMLine(" beq.b\t.rts \t\t;mem is same so video is same; don't change anything");
expect(asmLine.instructionRange).to.be.eql(new Range(new Position(0, 1), new Position(0, 6)));
expect(asmLine.dataRange).to.be.eql(new Range(new Position(0, 7), new Position(0, 11)));
expect(asmLine.commentRange).to.be.eql(new Range(new Position(0, 14), new Position(0, 66)));
asmLine = new ASMLine(".rts rts");
expect(asmLine.labelRange).to.be.eql(new Range(new Position(0, 0), new Position(0, 4)));
expect(asmLine.instructionRange).to.be.eql(new Range(new Position(0, 5), new Position(0, 8)));
});
it("Should parse a line without label", function () {
let asmLine = new ASMLine("\t\tmove.l #mempos,d1 ; mycomment");
Expand Down
2 changes: 1 addition & 1 deletion syntaxes/M68k-Assembly.YAML-tmLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,4 @@ patterns:
match: (?i)\billegal\b

- name: macro.m68k
match: "(?i)^([a-zA-Z0-9_][a-zA-Z0-9_\\.]*)"
match: (?i)^([a-zA-Z0-9_][a-zA-Z0-9_\\.]*)\b
2 changes: 1 addition & 1 deletion syntaxes/M68k-Assembly.tmLanguage.json
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@
},
{
"name": "macro.m68k",
"match": "(?i)^([a-zA-Z0-9_][a-zA-Z0-9_\\.]*)"
"match": "(?i)^([a-zA-Z0-9_][a-zA-Z0-9_\\\\.]*)\\b"
}
]
}
2 changes: 1 addition & 1 deletion test_files/hw2-toform.s
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ start
move.l #Message,d2
moveq #MessageEnd-Message,d3 ; comment 1
jsr _LVOWrite(a6)
move.l a6,a1
move.l a6,a1
move.l 4,a6
jsr _LVOCloseLibrary(a6)
moveq #0,d0
Expand Down

0 comments on commit 029e379

Please sign in to comment.