All files / src/modules/implementations/journal/syntax amount-parser.js

100% Statements 160/160
98.95% Branches 95/96
100% Functions 16/16
100% Lines 160/160

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 16167x 67x 67x 803x 803x 803x 803x 803x 803x 803x 803x 803x 803x 803x 803x 803x 803x 803x 535200x 535200x 803x 127844x 127844x 127844x 127844x 127844x 127844x 127844x 127844x 127844x 127844x 127844x 127844x 127844x 399071x 399071x 399071x 399071x 399071x 399071x 399071x 271241x 399071x 30x 30x 30x 399071x 1043x 1043x 206x 206x 206x 837x 837x 399071x 6312x 6312x 6312x 6312x 6312x 6312x 6312x 6312x 6312x 6312x 6312x 6312x 263856x 399071x 399071x 263856x 399071x 399071x 263856x 399071x 274x 274x 274x 274x 274x 399071x 131929x 399071x 131927x 131927x 131927x 131929x 399071x 399071x 131929x 399071x 131921x 131921x 399071x 127844x 22x 22x 127844x 803x 127844x 127844x 127844x 127844x 127844x 127844x 127844x 127844x 127844x 131931x 131931x 131927x 131931x 8x 8x 131917x 131917x 131931x 127844x 127840x 127840x 127840x 14x 14x 14x 14x 14x 127702x 127702x 127702x 127702x 127840x 3156x 3156x 3156x 3156x 2x 2x 3154x 3154x 127840x 1043x 1043x 1043x 127840x 16x 16x 16x 127686x 127686x 127840x 127844x 127698x 127698x 127698x 127844x 803x 127844x 127844x 803x 67x  
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
module.exports = ({ syntaxErrors: { syntaxError }, }) => {
    const TOKEN = Object.freeze({
        NUMBER: 1,
        SYMBOL: 2,
        AT: 3,
        TOTAL_AT: 4,
        EQUALS: 5,
        LEFT_BRACE: 6,
        TOTAL_LEFT_BRACE: 7,
        RIGHT_BRACE: 8,
        TOTAL_RIGHT_BRACE: 9,
        EOF: 10,
    });
    function isSpace(code) { return code === 32 || code === 9; }
    function isDigit(code) { return code >= 48 && code <= 57; }
    function isCommodityCharacter(code) {
        return code === 95 || /\p{L}/u.test(String.fromCodePoint(code));
    }
    class AmountLexer {
        baseColumn;
        input;
        line;
        offset;
        source;
        constructor(input, source, line, baseColumn) {
            this.input = input;
            this.source = source;
            this.line = line;
            this.baseColumn = baseColumn;
            this.offset = 0;
        }
        next() {
            const { input } = this;
            const length = input.length;
            while (this.offset < length && isSpace(input.charCodeAt(this.offset)))
                this.offset++;
            const start = this.offset;
            if (start === length)
                return { type: TOKEN.EOF, value: '', start };
            const code = input.charCodeAt(this.offset);
            if (code === 61) {
                this.offset++;
                return { type: TOKEN.EQUALS, value: '=', start };
            }
            if (code === 64) {
                this.offset++;
                if (input.charCodeAt(this.offset) === 64) {
                    this.offset++;
                    return { type: TOKEN.TOTAL_AT, value: '@@', start };
                }
                return { type: TOKEN.AT, value: '@', start };
            }
            if (code === 123 || code === 125) {
                this.offset++;
                const doubled = input.charCodeAt(this.offset) === code;
                if (doubled)
                    this.offset++;
                return {
                    type: code === 123
                        ? (doubled ? TOKEN.TOTAL_LEFT_BRACE : TOKEN.LEFT_BRACE)
                        : (doubled ? TOKEN.TOTAL_RIGHT_BRACE : TOKEN.RIGHT_BRACE),
                    value: input.slice(start, this.offset),
                    start,
                };
            }
            let cursor = this.offset;
            if (code === 43 || code === 45)
                cursor++;
            const integerStart = cursor;
            while (cursor < length && isDigit(input.charCodeAt(cursor)))
                cursor++;
            let validNumber = cursor > integerStart;
            if (input.charCodeAt(cursor) === 46) {
                const fractionStart = ++cursor;
                while (cursor < length && isDigit(input.charCodeAt(cursor)))
                    cursor++;
                validNumber = validNumber && cursor > fractionStart;
            }
            if (validNumber && (cursor === length || isSpace(input.charCodeAt(cursor)) ||
                input.charCodeAt(cursor) === 61 || input.charCodeAt(cursor) === 64 ||
                input.charCodeAt(cursor) === 123 || input.charCodeAt(cursor) === 125)) {
                this.offset = cursor;
                return { type: TOKEN.NUMBER, value: input.slice(start, cursor), start };
            }
            cursor = start;
            while (cursor < length && isCommodityCharacter(input.charCodeAt(cursor)))
                cursor++;
            if (cursor === start)
                this.error('Invalid commodity symbol character', start);
            this.offset = cursor;
            return { type: TOKEN.SYMBOL, value: input.slice(start, cursor), start };
        }
        error(message, offset) {
            throw syntaxError(message, this.source, this.line, this.baseColumn + offset);
        }
    }
    class AmountParser {
        current;
        lexer;
        constructor(input, source, line, baseColumn) {
            this.lexer = new AmountLexer(input, source, line, baseColumn);
            this.current = this.lexer.next();
        }
        advance() { const token = this.current; this.current = this.lexer.next(); return token; }
        currentType() { return this.current.type; }
        parseAmount(label) {
            if (this.current.type !== TOKEN.NUMBER)
                this.lexer.error(`Expected a number for ${label}`, this.current.start);
            const quantity = this.advance().value;
            if (this.currentType() !== TOKEN.SYMBOL) {
                this.lexer.error(`Expected a commodity symbol for ${label}`, this.current.start);
            }
            const commodity = this.advance().value;
            return { quantity, commodity };
        }
        parse() {
            if (this.current.type === TOKEN.EOF)
                return null;
            if (this.current.type === TOKEN.EQUALS) {
                this.advance();
                const balanceAssignment = this.parseAmount('balance assignment');
                this.expectEnd();
                return { amount: null, lotCost: null, cost: null, balanceAssignment, balanceAssertion: null };
            }
            const amount = this.parseAmount('posting amount');
            let lotCost = null;
            let cost = null;
            let balanceAssertion = null;
            if (this.current.type === TOKEN.LEFT_BRACE || this.current.type === TOKEN.TOTAL_LEFT_BRACE) {
                const total = this.advance().type === TOKEN.TOTAL_LEFT_BRACE;
                lotCost = { total, amount: this.parseAmount('lot cost') };
                const closing = total ? TOKEN.TOTAL_RIGHT_BRACE : TOKEN.RIGHT_BRACE;
                if (this.currentType() !== closing) {
                    this.lexer.error(`Expected ${JSON.stringify(total ? '}}' : '}')} after lot cost`, this.current.start);
                }
                this.advance();
            }
            if (this.current.type === TOKEN.AT || this.current.type === TOKEN.TOTAL_AT) {
                const total = this.advance().type === TOKEN.TOTAL_AT;
                cost = { total, amount: this.parseAmount('cost') };
            }
            if (this.current.type === TOKEN.EQUALS) {
                this.advance();
                balanceAssertion = this.parseAmount('balance assertion');
            }
            this.expectEnd();
            return { amount, lotCost, cost, balanceAssignment: null, balanceAssertion };
        }
        expectEnd() {
            if (this.current.type !== TOKEN.EOF)
                this.lexer.error(`Unexpected token ${JSON.stringify(this.current.value)}`, this.current.start);
        }
    }
    function parseAmountExpression(input, location) {
        return new AmountParser(input, location.source, location.line, location.column).parse();
    }
    return { parseAmountExpression };
};