/* eslint-disable max-lines */ /* eslint-disable sort-keys */ // Maybe later /* eslint-disable complexity */ // Temporary ignore while switching linter /* eslint-disable no-param-reassign */ // Also temporary import {combine, last} from '../core/ArrayUtilities.mjs'; import Tokeniser from './Tokeniser.mjs'; import labelPatternParser from './LabelPatternParser.mjs'; import markdownParser from './MarkdownParser.mjs'; const BLOCK_TYPES = { 'if': { type: 'block begin', blockType: 'if', tag: 'if', skip: [], }, 'else': { type: 'block split', blockType: 'else', tag: 'else', skip: ['if'], }, 'repeat': { type: 'block begin', blockType: 'repeat', tag: 'repeat', skip: [], }, 'group': { type: 'block begin', blockType: 'group', tag: '', skip: [], }, }; const CONNECT = { types: ((() => { const lTypes = [ {tok: '', type: 0}, {tok: '<', type: 1}, {tok: '<<', type: 2}, ]; const mTypes = [ {tok: '-', type: 'solid'}, {tok: '--', type: 'dash'}, {tok: '~', type: 'wave'}, ]; const rTypes = [ {tok: '', type: 0}, {tok: '>', type: 1}, {tok: '>>', type: 2}, {tok: 'x', type: 3}, ]; const arrows = (combine([lTypes, mTypes, rTypes]) .filter((arrow) => (arrow[0].type !== 0 || arrow[2].type !== 0)) ); const types = new Map(); arrows.forEach((arrow) => { types.set(arrow.map((part) => part.tok).join(''), { line: arrow[1].type, left: arrow[0].type, right: arrow[2].type, }); }); return types; })()), agentFlags: { '*': {flag: 'begin', allowBlankName: true, blankNameFlag: 'source'}, '+': {flag: 'start'}, '-': {flag: 'stop'}, '!': {flag: 'end'}, }, }; const TERMINATOR_TYPES = [ 'none', 'box', 'cross', 'fade', 'bar', ]; const NOTE_TYPES = { 'text': { mode: 'text', types: { 'left': {type: 'note left', skip: ['of'], min: 0, max: null}, 'right': {type: 'note right', skip: ['of'], min: 0, max: null}, }, }, 'note': { mode: 'note', types: { 'over': {type: 'note over', skip: [], min: 0, max: null}, 'left': {type: 'note left', skip: ['of'], min: 0, max: null}, 'right': {type: 'note right', skip: ['of'], min: 0, max: null}, 'between': {type: 'note between', skip: [], min: 2, max: null}, }, }, 'state': { mode: 'state', types: { 'over': {type: 'note over', skip: [], min: 1, max: 1}, }, }, }; const DIVIDER_TYPES = { 'line': {defaultHeight: 6}, 'space': {defaultHeight: 6}, 'delay': {defaultHeight: 30}, 'tear': {defaultHeight: 6}, }; const AGENT_MANIPULATION_TYPES = { 'define': {type: 'agent define'}, 'begin': {type: 'agent begin', mode: 'box'}, 'end': {type: 'agent end', mode: 'cross'}, }; function makeError(message, token = null) { let suffix = ''; if(token) { suffix = ( ' at line ' + (token.b.ln + 1) + ', character ' + token.b.ch ); } return new Error(message + suffix); } function joinLabel(line, begin = 0, end = null) { if(end === null) { end = line.length; } if(end <= begin) { return ''; } let result = line[begin].v; for(let i = begin + 1; i < end; ++ i) { result += line[i].s + line[i].v; } return result; } function readNumber(line, begin = 0, end = null, def = Number.NAN) { const text = joinLabel(line, begin, end); return Number(text || def); } function tokenKeyword(token) { if(!token || token.q) { return null; } return token.v; } function skipOver(line, start, skip, error = null) { for(let i = 0; i < skip.length; ++ i) { const expected = skip[i]; const token = line[start + i]; if(tokenKeyword(token) !== expected) { if(error) { throw makeError( error + '; expected "' + expected + '"', token ); } else { return start; } } } return start + skip.length; } function findToken(line, tokens, { start = 0, limit = null, orEnd = false, } = {}) { if(limit === null) { limit = line.length; } if(!Array.isArray(tokens)) { tokens = [tokens]; } for(let i = start; i <= limit - tokens.length; ++ i) { if(skipOver(line, i, tokens) !== i) { return i; } } return orEnd ? limit : -1; } function findFirstToken(line, tokenMap, {start = 0, limit = null} = {}) { if(limit === null) { limit = line.length; } for(let pos = start; pos < limit; ++ pos) { const value = tokenMap.get(tokenKeyword(line[pos])); if(value) { return {pos, value}; } } return null; } function readAgentAlias(line, start, end, {enableAlias, allowBlankName}) { let aliasSep = -1; if(enableAlias) { aliasSep = findToken(line, 'as', {start}); } if(aliasSep === -1 || aliasSep >= end) { aliasSep = end; } if(start >= aliasSep && !allowBlankName) { let errPosToken = line[start]; if(!errPosToken) { errPosToken = {b: last(line).e}; } throw makeError('Missing agent name', errPosToken); } return { name: joinLabel(line, start, aliasSep), alias: joinLabel(line, aliasSep + 1, end), }; } function readAgent(line, start, end, { flagTypes = {}, aliases = false, } = {}) { const flags = []; const blankNameFlags = []; let p = start; let allowBlankName = false; for(; p < end; ++ p) { const token = line[p]; const rawFlag = tokenKeyword(token); const flag = flagTypes[rawFlag]; if(!flag) { break; } if(flags.includes(flag.flag)) { throw makeError('Duplicate agent flag: ' + rawFlag, token); } allowBlankName = allowBlankName || Boolean(flag.allowBlankName); flags.push(flag.flag); blankNameFlags.push(flag.blankNameFlag); } const {name, alias} = readAgentAlias(line, p, end, { enableAlias: aliases, allowBlankName, }); return { name, alias, flags: name ? flags : blankNameFlags, }; } function readAgentList(line, start, end, readAgentOpts) { const list = []; let currentStart = -1; for(let i = start; i < end; ++ i) { const token = line[i]; if(tokenKeyword(token) === ',') { if(currentStart !== -1) { list.push(readAgent(line, currentStart, i, readAgentOpts)); currentStart = -1; } } else if(currentStart === -1) { currentStart = i; } } if(currentStart !== -1) { list.push(readAgent(line, currentStart, end, readAgentOpts)); } return list; } const PARSERS = [ (line, meta) => { // Title if(tokenKeyword(line[0]) !== 'title') { return null; } meta.title = joinLabel(line, 1); return true; }, (line, meta) => { // Theme if(tokenKeyword(line[0]) !== 'theme') { return null; } meta.theme = joinLabel(line, 1); return true; }, (line, meta) => { // Terminators if(tokenKeyword(line[0]) !== 'terminators') { return null; } const type = tokenKeyword(line[1]); if(!type) { throw makeError('Unspecified termination', line[0]); } if(TERMINATOR_TYPES.indexOf(type) === -1) { throw makeError('Unknown termination "' + type + '"', line[1]); } meta.terminators = type; return true; }, (line, meta) => { // Headers if(tokenKeyword(line[0]) !== 'headers') { return null; } const type = tokenKeyword(line[1]); if(!type) { throw makeError('Unspecified header', line[0]); } if(TERMINATOR_TYPES.indexOf(type) === -1) { throw makeError('Unknown header "' + type + '"', line[1]); } meta.headers = type; return true; }, (line) => { // Divider if(tokenKeyword(line[0]) !== 'divider') { return null; } const labelSep = findToken(line, ':', {orEnd: true}); const heightSep = findToken(line, ['with', 'height'], { limit: labelSep, orEnd: true, }); const mode = joinLabel(line, 1, heightSep) || 'line'; if(!DIVIDER_TYPES[mode]) { throw makeError('Unknown divider type', line[1]); } const height = readNumber( line, heightSep + 2, labelSep, DIVIDER_TYPES[mode].defaultHeight ); if(Number.isNaN(height) || height < 0) { throw makeError('Invalid divider height', line[heightSep + 2]); } return { type: 'divider', mode, height, label: joinLabel(line, labelSep + 1), }; }, (line) => { // Autolabel if(tokenKeyword(line[0]) !== 'autolabel') { return null; } let raw = null; if(tokenKeyword(line[1]) === 'off') { raw = '