From 4d6653dde7a1eb91ef0ea7f24ca92ba455f8befa Mon Sep 17 00:00:00 2001 From: Matt Godbolt Date: Mon, 12 Nov 2018 22:10:31 -0600 Subject: [PATCH] Handle strings with spaces in them in asm output with horizontal whitespace squashing on. Fixes #883 --- lib/asmregex.js | 17 ++++++++++++++--- test/asm-tests.js | 21 +++++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/lib/asmregex.js b/lib/asmregex.js index c35b40c87..47aaccac4 100644 --- a/lib/asmregex.js +++ b/lib/asmregex.js @@ -23,21 +23,32 @@ // POSSIBILITY OF SUCH DAMAGE. "use strict"; +const findQuotes = /(.*?)("(?:[^"\\]|\\.)*")(.*)/; + class AsmRegex { constructor() { this.labelDef = /^([.a-z_$@][a-z0-9$_@.]*):/i; } - static filterAsmLine(line, filters) { - if (!filters.trim) return line; + static squashHorizontalWhitespace(line, atStart) { + const quotes = line.match(findQuotes); + if (quotes) { + return this.squashHorizontalWhitespace(quotes[1], atStart) + quotes[2] + + this.squashHorizontalWhitespace(quotes[3], false); + } const splat = line.split(/\s+/); - if (splat[0] === "") { + if (splat[0] === "" && atStart) { // An indented line: preserve a two-space indent return " " + splat.slice(1).join(" "); } else { return splat.join(" "); } } + + static filterAsmLine(line, filters) { + if (!filters.trim) return line; + return this.squashHorizontalWhitespace(line, true); + } } exports.AsmRegex = AsmRegex; diff --git a/test/asm-tests.js b/test/asm-tests.js index f37fbc901..d58cd0059 100644 --- a/test/asm-tests.js +++ b/test/asm-tests.js @@ -23,6 +23,7 @@ // POSSIBILITY OF SUCH DAMAGE. const AsmParser = require('../lib/asm-parser-vc'); +const AsmRegex = require('../lib/asmregex').AsmRegex; require('chai').should(); describe('ASM CL parser', () => { @@ -36,3 +37,23 @@ describe('ASM CL parser', () => { ); }); }); + + +describe('ASM regex base class', () => { + it('should leave unfiltered lines alone', () => { + const line = " this is a line"; + AsmRegex.filterAsmLine(line, {}).should.equal(line); + }); + it('should use up internal whitespace when asked', () => { + AsmRegex.filterAsmLine(" this is a line", {trim: true}).should.equal(" this is a line"); + AsmRegex.filterAsmLine("this is a line", {trim: true}).should.equal("this is a line"); + }); + it('should keep whitespace in strings', () => { + AsmRegex.filterAsmLine('equs "this string"', {trim: true}).should.equal('equs "this string"'); + AsmRegex.filterAsmLine(' equs "this string"', {trim: true}).should.equal(' equs "this string"'); + AsmRegex.filterAsmLine('equs "this \\" string \\""', {trim: true}).should.equal('equs "this \\" string \\""'); + }); + it('should not get upset by mismatched strings', () => { + AsmRegex.filterAsmLine("a \"string 'yeah", {trim: true}).should.equal("a \"string 'yeah"); + }); +}); \ No newline at end of file