Skip to content

Commit

Permalink
Consolidate LocationParser into SymbolParser
Browse files Browse the repository at this point in the history
  • Loading branch information
tgtakaoka committed Oct 16, 2024
1 parent c1391a1 commit bd58ae4
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 141 deletions.
1 change: 0 additions & 1 deletion src/asm_cdp1802.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ const ValueParser::Plugins &AsmCdp1802::defaultPlugins() {
const CommentParser &comment() const override { return RcaCommentParser::singleton(); }
const SymbolParser &symbol() const override { return RcaSymbolParser::singleton(); }
const LetterParser &letter() const override { return RcaLetterParser::singleton(); }
const LocationParser &location() const override { return StarLocationParser::singleton(); }
const FunctionTable &function() const override { return RcaFunctionTable::singleton(); }
} PLUGINS{};
return PLUGINS;
Expand Down
3 changes: 0 additions & 3 deletions src/asm_ins8060.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ const ValueParser::Plugins &AsmIns8060::defaultPlugins() {
static const struct final : ValueParser::Plugins {
const NumberParser &number() const override { return Ins80xxNumberParser::singleton(); }
const SymbolParser &symbol() const override { return Ins80xxSymbolParser::singleton(); }
const LocationParser &location() const override {
return DotDollarLocationParser::singleton();
}
const FunctionTable &function() const override { return Ins8060FunctionTable::singleton(); }
} PLUGINS{};
return PLUGINS;
Expand Down
3 changes: 0 additions & 3 deletions src/asm_ins8070.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ const ValueParser::Plugins &AsmIns8070::defaultPlugins() {
static const struct final : ValueParser::Plugins {
const NumberParser &number() const override { return Ins80xxNumberParser::singleton(); }
const SymbolParser &symbol() const override { return Ins80xxSymbolParser::singleton(); }
const LocationParser &location() const override {
return DotDollarLocationParser::singleton();
}
const FunctionTable &function() const override { return Ins8070FunctionTable::singleton(); }
} PLUGINS{};
return PLUGINS;
Expand Down
3 changes: 3 additions & 0 deletions src/asm_mc68000.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ PROGMEM constexpr Pseudos PSEUDO_TABLE{ARRAY_RANGE(PSEUDOS)};

struct Mc68000SymbolParser final : PrefixSymbolParser, Singleton<Mc68000SymbolParser> {
Mc68000SymbolParser() : PrefixSymbolParser(PSTR_DOT, PSTR_UNDER_DOT_DOLLAR) {}
bool locationSymbol(StrScanner &scan) const override {
return SymbolParser::locationSymbol(scan, '*');
}
bool instructionLetter(char c) const override {
return PrefixSymbolParser::instructionLetter(c) || c == '.';
}
Expand Down
8 changes: 7 additions & 1 deletion src/asm_mn1610.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,20 @@ struct Mn1610NumberParser final : IbmNumberParser, Singleton<Mn1610NumberParser>
Mn1610NumberParser() : IbmNumberParser('X', 0, 0, 0) {}
};

struct Mn1610SymbolParser final : SymbolParser, Singleton<Mn1610SymbolParser> {
bool locationSymbol(StrScanner &scan) const override {
return SymbolParser::locationSymbol(scan, '*');
}
};

} // namespace

const ValueParser::Plugins &AsmMn1610::defaultPlugins() {
static const struct final : ValueParser::Plugins {
const NumberParser &number() const override { return Mn1610NumberParser::singleton(); }
const SymbolParser &symbol() const override { return Mn1610SymbolParser::singleton(); }
const CommentParser &comment() const override { return StarCommentParser::singleton(); }
const LetterParser &letter() const override { return IbmLetterParser::singleton(); }
const LocationParser &location() const override { return StarLocationParser::singleton(); }
} PLUGINS{};
return PLUGINS;
}
Expand Down
7 changes: 4 additions & 3 deletions src/asm_mos6502.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ using namespace text::common;
using namespace text::mos6502;

namespace {

// clang-format off
constexpr char TEXT_aequal[] PROGMEM = "*=";

// clang-format off
constexpr Pseudo PSEUDOS[] PROGMEM = {
{TEXT_aequal, &Assembler::defineOrigin},
{TEXT_dALIGN, &Assembler::alignOrigin},
Expand All @@ -58,6 +57,9 @@ struct MostekCommentParser final : CommentParser, Singleton<MostekCommentParser>

struct MostekSymbolParser final : SimpleSymbolParser, Singleton<MostekSymbolParser> {
MostekSymbolParser() : SimpleSymbolParser(PSTR_UNDER) {}
bool locationSymbol(StrScanner &scan) const override {
return SymbolParser::locationSymbol(scan, '*');
}
bool instructionLetter(char c) const override {
return SymbolParser::instructionLetter(c) || c == '.' || c == '=' || c == '*' || c == ':';
}
Expand All @@ -82,7 +84,6 @@ const ValueParser::Plugins &AsmMos6502::defaultPlugins() {
const CommentParser &comment() const override { return MostekCommentParser::singleton(); }
const SymbolParser &symbol() const override { return MostekSymbolParser::singleton(); }
const LetterParser &letter() const override { return MostekLetterParser::singleton(); }
const LocationParser &location() const override { return StarLocationParser::singleton(); }
} PLUGINS{};
return PLUGINS;
}
Expand Down
8 changes: 4 additions & 4 deletions src/asm_ns32000.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ struct Ns32000NumberParser final : NationalNumberParser, Singleton<Ns32000Number

struct Ns32000SymbolParser final : SimpleSymbolParser, Singleton<Ns32000SymbolParser> {
Ns32000SymbolParser() : SimpleSymbolParser(PSTR_UNDER_DOT) {}
bool locationSymbol(StrScanner &scan) const override {
return SymbolParser::locationSymbol(scan, '.') || SymbolParser::locationSymbol(scan, '*');
}
bool instructionLetter(char c) const override {
return SimpleSymbolParser::instructionLetter(c) || c == '.';
}
Expand All @@ -96,9 +99,6 @@ const ValueParser::Plugins &AsmNs32000::defaultPlugins() {
const CommentParser &comment() const override { return SharpCommentParser::singleton(); }
const SymbolParser &symbol() const override { return Ns32000SymbolParser::singleton(); }
const LetterParser &letter() const override { return Ns32000LetterParser::singleton(); }
const LocationParser &location() const override {
return DotStarLocationParser::singleton();
}
} PLUGINS{};
return PLUGINS;
}
Expand Down Expand Up @@ -244,7 +244,7 @@ Error AsmNs32000::parseBaseOperand(StrScanner &scan, Operand &op) const {
}

auto l = p;
if (parser().locationSymbol(l)) {
if (Ns32000SymbolParser::singleton().locationSymbol(l)) {
op.val = parseInteger(p, op);
if (op.hasError())
return op.getError();
Expand Down
6 changes: 3 additions & 3 deletions src/asm_pdp8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ constexpr Pseudo PSEUDOS[] PROGMEM = {
PROGMEM constexpr Pseudos PSEUDO_TABLE{ARRAY_RANGE(PSEUDOS)};

struct Pdp8SymbolParser final : SymbolParser, Singleton<Pdp8SymbolParser> {
bool locationSymbol(StrScanner &scan) const override {
return SymbolParser::locationSymbol(scan, '.') || SymbolParser::locationSymbol(scan, '$');
}
bool labelDelimitor(StrScanner &scan) const override { return scan.expect(','); }
bool instructionLetter(char c) const override {
return SymbolParser::symbolLetter(c) || c == '*' || c == '=';
Expand Down Expand Up @@ -106,9 +109,6 @@ const ValueParser::Plugins &AsmPdp8::defaultPlugins() {
const LetterParser &letter() const override { return Pdp8LetterParser::singleton(); }
const CommentParser &comment() const override { return Pdp8CommentParser::singleton(); }
const OperatorParser &operators() const override { return Pdp8OperatorParser::singleton(); }
const LocationParser &location() const override {
return DotDollarLocationParser::singleton();
}
} PLUGINS{};
return PLUGINS;
}
Expand Down
4 changes: 2 additions & 2 deletions src/asm_scn2650.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ struct SigneticsNumberParser final : IbmNumberParser, Singleton<SigneticsNumberP
};

struct SigneticsLetterParser final : LetterParser, Singleton<SigneticsLetterParser> {
bool letterPrefix(StrScanner &scan) const override { return scan.iexpect('A') != 0; }
bool stringPrefix(StrScanner &scan) const override { return scan.iexpect('A') != 0; }
bool letterPrefix(StrScanner &scan) const override { return scan.iexpect('A'); }
bool stringPrefix(StrScanner &scan) const override { return scan.iexpect('A'); }
};

} // namespace
Expand Down
76 changes: 0 additions & 76 deletions src/location_parser.h

This file was deleted.

1 change: 0 additions & 1 deletion src/parsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#include "comment_parser.h"
#include "letter_parser.h"
#include "location_parser.h"
#include "number_parser.h"
#include "symbol_parser.h"

Expand Down
19 changes: 19 additions & 0 deletions src/symbol_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ struct SymbolParser {
return isalpha(c) || (!headOfSymbol && isdigit(c)) || c == '_';
}

/** Default location symbol is '$' */
virtual bool locationSymbol(StrScanner &scan) const { return locationSymbol(scan, '$'); }

/** Label may have delimitor letter */
virtual bool labelDelimitor(StrScanner &scan) const { return scan.expect(':'); }

Expand All @@ -43,6 +46,16 @@ struct SymbolParser {

/** Function name is same as symbol */
virtual bool functionNameLetter(char c) const { return symbolLetter(c); }

protected:
bool locationSymbol(StrScanner &scan, char location) const {
auto p = scan;
if (p.expect(location) && !symbolLetter(*p)) {
scan = p;
return true;
}
return false;
}
};

/**
Expand Down Expand Up @@ -86,10 +99,16 @@ struct UnderQuestionSymbolParser final : PrefixSymbolParser, Singleton<UnderQues
struct Mc68xxSymbolParser final : PrefixSymbolParser, Singleton<Mc68xxSymbolParser> {
Mc68xxSymbolParser()
: PrefixSymbolParser(text::common::PSTR_DOT, text::common::PSTR_UNDER_DOT_DOLLAR) {}
bool locationSymbol(StrScanner &scan) const override {
return SymbolParser::locationSymbol(scan, '*');
}
};

struct Ins80xxSymbolParser final : PrefixSymbolParser, Singleton<Ins80xxSymbolParser> {
Ins80xxSymbolParser() : PrefixSymbolParser(text::common::PSTR_DOLLAR) {}
bool locationSymbol(StrScanner &scan) const override {
return SymbolParser::locationSymbol(scan, '.') || SymbolParser::locationSymbol(scan, '$');
}
bool instructionLetter(char c) const override {
return PrefixSymbolParser::instructionLetter(c) || c == '=' || c == '.';
}
Expand Down
32 changes: 16 additions & 16 deletions src/value_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
* limitations under the License.
*/

#include "value_parser.h"
#include <ctype.h>
#include <stdlib.h>
#include "config_base.h"
#include "stack.h"
#include "text_common.h"
#include "value_parser.h"

namespace libasm {

Expand All @@ -41,10 +41,6 @@ const LetterParser &ValueParser::Plugins::letter() const {
return SINGLETON;
}

const LocationParser &ValueParser::Plugins::location() const {
return DollarLocationParser::singleton();
}

const OperatorParser &ValueParser::Plugins::operators() const {
return CStyleOperatorParser::singleton();
}
Expand Down Expand Up @@ -80,18 +76,19 @@ const ValueParser::Plugins &ValueParser::Plugins::motorola() {
const NumberParser &ValueParser::MotorolaPlugins::number() const {
return MotorolaNumberParser::singleton();
}

const CommentParser &ValueParser::MotorolaPlugins::comment() const {
return StarCommentParser::singleton();
}

const SymbolParser &ValueParser::MotorolaPlugins::symbol() const {
return Mc68xxSymbolParser::singleton();
}

const LetterParser &ValueParser::MotorolaPlugins::letter() const {
return MotorolaLetterParser::singleton();
}
const LocationParser &ValueParser::MotorolaPlugins::location() const {
return StarLocationParser::singleton();
}

const OperatorParser &ValueParser::MotorolaPlugins::operators() const {
return Mc68xxOperatorParser::singleton();
}
Expand All @@ -116,6 +113,13 @@ struct FairchildNumberParser final : NumberParser, Singleton<FairchildNumberPars
IbmNumberParser _ibm{'H', 'B', 'O', 'D'};
};

struct FairchildSymbolParser final : SymbolParser, Singleton<FairchildSymbolParser> {
bool locationSymbol(StrScanner &scan) const override {
return SymbolParser::locationSymbol(scan, '*') || SymbolParser::locationSymbol(scan, '$') ||
SymbolParser::locationSymbol(scan, '.');
}
};

struct FairchildLetterParser final : LetterParser, Singleton<FairchildLetterParser> {
/** Fairchild style letter is [cC]'[:print:]', #[:print:], '[:print:]'? */
Error parseLetter(StrScanner &scan, char &letter) const override {
Expand Down Expand Up @@ -152,24 +156,20 @@ struct FairchildLetterParser final : LetterParser, Singleton<FairchildLetterPars
}
};

struct FairchildLocationParser final : SimpleLocationParser, Singleton<FairchildLocationParser> {
FairchildLocationParser() : SimpleLocationParser(text::common::PSTR_DOT_STAR_DOLLAR) {}
};

} // namespace fairchild

const ValueParser::Plugins &ValueParser::Plugins::fairchild() {
static const struct final : ValueParser::Plugins {
const NumberParser &number() const override {
return fairchild::FairchildNumberParser::singleton();
}
const SymbolParser &symbol() const override {
return fairchild::FairchildSymbolParser::singleton();
}
const CommentParser &comment() const override { return StarCommentParser::singleton(); }
const LetterParser &letter() const override {
return fairchild::FairchildLetterParser::singleton();
}
const LocationParser &location() const override {
return fairchild::FairchildLocationParser::singleton();
}
} PLUGINS{};
return PLUGINS;
}
Expand Down Expand Up @@ -457,7 +457,7 @@ Error ValueParser::parseConstant(StrScanner &scan, Value &val, ParserContext &co
return err;
}

if (_location.locationSymbol(p)) {
if (_symbol.locationSymbol(p)) {
val.setUnsigned(context.currentLocation);
scan = p;
return OK;
Expand Down
Loading

0 comments on commit bd58ae4

Please sign in to comment.