@scalenc/nc-format
Version:
Library for handling TRUMPF NC file format.
107 lines • 4 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BlockReader = void 0;
const Block_1 = require("../models/Block");
const Constants_1 = require("./Constants");
const Expressions_1 = require("../models/Expressions");
const Assignment_1 = require("../models/Statements/Assignment");
const Instruction_1 = require("../models/Statements/Instruction");
const Errors_1 = require("./Errors");
const ParserException_1 = require("./ParserException");
const StatementReader_1 = require("./StatementReader");
const Token_1 = require("./Token");
class BlockReader {
parser;
statementReader;
statement;
constructor(parser) {
this.parser = parser;
this.statementReader = new StatementReader_1.StatementReader(parser);
}
tryRead() {
if (this.isEndOfFile()) {
return undefined;
}
if (this.isEndOfLine()) {
// Special handling for empty lines, since otherwise, line number is not accounted correctly.
const line = this.parser.line - 1;
this.skipLineEnd();
return new Block_1.Block(line);
}
const line = this.parser.line;
this.statement = this.statementReader.tryRead();
const blockNumber = this.tryReadBlockNumber();
const labels = this.readAllLabels();
const statements = this.readAllStatements();
const comment = this.tryReadComment();
this.skipLineEnd();
return new Block_1.Block(line, blockNumber, labels, statements, comment);
}
tryReadBlockNumber() {
if (this.statement instanceof Assignment_1.Assignment && this.statement.variable.toUpperCase() === Constants_1.Constants.NUMBER_TOKEN) {
if (this.statement.expression instanceof Expressions_1.Number && this.statement.expression.isInteger) {
const blockNumber = this.statement.expression.value;
this.statement = this.statementReader.tryRead();
return blockNumber;
}
throw new ParserException_1.ParserException(this.parser, Errors_1.Errors.EXPECTED_BLOCK_NUMBER_BUT_FOUND_EXPRESSION);
}
return undefined;
}
readAllLabels() {
const labels = [];
for (;;) {
const label = this.tryReadLabel();
if (!label) {
return labels;
}
labels.push(label);
}
}
tryReadLabel() {
if (this.statement instanceof Instruction_1.Instruction && !this.statement.expressions && this.isLabelToken(this.parser.token)) {
const label = this.statement.name;
this.parser.tryRead(); // Skip ':'.
this.statement = this.statementReader.tryRead();
return label;
}
return undefined;
}
readAllStatements() {
if (!this.statement) {
return undefined;
}
const statements = [];
do {
statements.push(this.statement);
this.statement = this.statementReader.tryRead();
} while (this.statement);
return statements;
}
tryReadComment() {
if (this.parser.token?.type === Token_1.TokenType.COMMENT) {
const comment = this.parser.token.value;
this.parser.tryRead();
return comment;
}
}
skipLineEnd() {
if (this.parser.token) {
if (this.parser.token.type !== Token_1.TokenType.NEW_LINE) {
throw new ParserException_1.ParserException(this.parser, Errors_1.Errors.EXPECTED_LINE_END);
}
this.parser.tryRead();
}
}
isEndOfFile() {
return !this.parser.token && !this.parser.tryRead();
}
isEndOfLine() {
return this.parser.token?.type === Token_1.TokenType.NEW_LINE;
}
isLabelToken(token) {
return token?.type === Token_1.TokenType.COLON;
}
}
exports.BlockReader = BlockReader;
//# sourceMappingURL=BlockReader.js.map