UNPKG

@scalenc/nc-format

Version:

Library for handling TRUMPF NC file format.

138 lines 6.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DeclarationReader = void 0; const Expressions_1 = require("../models/Expressions"); const Declaration_1 = require("../models/Statements/Declaration"); const Constants_1 = require("./Constants"); const Errors_1 = require("./Errors"); const ExpressionReader_1 = require("./ExpressionReader"); const ParserException_1 = require("./ParserException"); const Token_1 = require("./Token"); class DeclarationReader { parser; expressionReader; constructor(parser) { this.parser = parser; this.expressionReader = new ExpressionReader_1.ExpressionReader(parser); } readDeclaration() { this.parser.tryRead(); // Skip 'DEF'. this.parser.assertNotLineOrFileEnd(); const type = this.toVariableType(); this.parser.tryRead(); // Skip variable type. const declaration = new Declaration_1.Declaration(type); if (type === Declaration_1.VariableType.STRING && this.isFieldBeginToken(this.parser.token)) { declaration.stringLength = this.readStringLength(); } if (this.parser.token != null && this.isUnitToken(this.parser.token)) { this.parser.tryRead(); // Skip 'PHU' declaration.unit = this.parser.readIntegerNumber(); } if (this.parser.token != null && this.isLowerLimitToken(this.parser.token)) { this.parser.tryRead(); // Skip 'LLI' declaration.lowerLimit = this.readDoubleNumber(); } if (this.parser.token != null && this.isUpperLimitToken(this.parser.token)) { this.parser.tryRead(); // Skip 'ULI' declaration.upperLimit = this.readDoubleNumber(); } declaration.variables = this.readVariables(); return declaration; } readStringLength() { this.parser.tryRead(); // Skip '[' this.parser.assertNotLineOrFileEnd(); const stringLength = this.parser.readIntegerNumber(); if (!this.isFieldEndToken(this.parser.token)) { throw new ParserException_1.ParserException(this.parser, Errors_1.Errors.EXPECTED_CLOSING_FIELD_BRACE); } this.parser.tryRead(); // Skip ']' return stringLength; } readVariables() { this.parser.assertNotLineOrFileEnd(); const variables = []; for (;;) { if (this.parser.token?.type !== Token_1.TokenType.IDENTIFIER) { throw new ParserException_1.ParserException(this.parser, Errors_1.Errors.EXPECTED_VARIABLE_NAME); } variables.push(this.readVariable()); if (!this.isSeparatorToken(this.parser.token)) { break; } this.parser.tryRead(); // Skip ',' } return variables; } readVariable() { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const name = this.parser.token.value; const fieldLengths = this.parser.tryRead() && this.isFieldBeginToken(this.parser.token) ? this.readFieldLengths() : undefined; const initExpression = Token_1.isAssignmentOperator(this.parser.token) ? this.readInitExpression() : undefined; return { name, fieldLengths, initExpression }; } readFieldLengths() { const fieldLengths = []; do { this.parser.tryRead(); // Skip '[' or ',' fieldLengths.push(this.parser.readIntegerNumber()); } while (this.isSeparatorToken(this.parser.token)); if (!this.isFieldEndToken(this.parser.token)) { throw new ParserException_1.ParserException(this.parser, Errors_1.Errors.EXPECTED_CLOSING_FIELD_BRACE); } this.parser.tryRead(); // Skip ']' return fieldLengths; } readInitExpression() { this.parser.tryRead(); // Skip '='. return this.expressionReader.readExpression(); } toVariableType() { if (this.parser.token?.type === Token_1.TokenType.IDENTIFIER) { switch (this.parser.token.value.toUpperCase()) { case Constants_1.Constants.DEF_INT: return Declaration_1.VariableType.INT; case Constants_1.Constants.DEF_REAL: return Declaration_1.VariableType.REAL; case Constants_1.Constants.DEF_BOOL: return Declaration_1.VariableType.BOOL; case Constants_1.Constants.DEF_CHAR: return Declaration_1.VariableType.CHAR; case Constants_1.Constants.DEF_STRING: return Declaration_1.VariableType.STRING; case Constants_1.Constants.DEF_AXIS: return Declaration_1.VariableType.AXIS; case Constants_1.Constants.DEF_FRAME: return Declaration_1.VariableType.FRAME; } } throw new ParserException_1.ParserException(this.parser, Errors_1.Errors.EXPECTED_VARIABLE_TYPE); } readDoubleNumber() { const expression = this.expressionReader.readExpression(); if (expression instanceof Expressions_1.Number) { return expression.value; } throw new ParserException_1.ParserException(this.parser, Errors_1.Errors.EXPECTED_NUMBER); } isFieldBeginToken(token) { return token?.type === Token_1.TokenType.FIELD_BRACE && token.value.length === 1 && token.value[0] === Constants_1.Constants.DEF_FIELD_BEGIN; } isFieldEndToken(token) { return token?.type === Token_1.TokenType.FIELD_BRACE && token.value.length === 1 && token.value[0] === Constants_1.Constants.DEF_FIELD_END; } isUnitToken(token) { return token.type === Token_1.TokenType.IDENTIFIER && token.value === Constants_1.Constants.DEF_UNIT; } isLowerLimitToken(token) { return token.type === Token_1.TokenType.IDENTIFIER && token.value === Constants_1.Constants.DEF_LOWER_LIMIT; } isUpperLimitToken(token) { return token.type === Token_1.TokenType.IDENTIFIER && token.value === Constants_1.Constants.DEF_UPPER_LIMIT; } isSeparatorToken(token) { return token?.type === Token_1.TokenType.SEPARATOR && token.value.length === 1 && token.value[0] === Constants_1.Constants.DEF_SEPARATOR; } } exports.DeclarationReader = DeclarationReader; //# sourceMappingURL=DeclarationReader.js.map