/*----------------------------------------------------------------------------*/ /* */ /* Copyright (c) 2026 Rexx Language Association. All rights reserved. */ /* */ /* This program and the accompanying materials are made available under */ /* the terms of the Common Public License v1.0 which accompanies this */ /* distribution. A copy is also available at the following address: */ /* https://www.oorexx.org/license.html */ /* */ /*----------------------------------------------------------------------------*/ /* * commandwords.cls: how ADDRESS FETCH and ADDRESS DOM (fetch.cls, dom.cls) * split their commands. Words are separated by blanks; a word in quotes * ("..." or '...', the quote doubled inside) may hold blanks and is never * taken for a keyword; "INTO name", anywhere, names the variable for the * command's value; "FROM name" takes a value from a variable. * * w = .CommandWords~new('TEXT "Hello, world" INTO x') * if w~error \== .nil then ... -- e.g. a quote not closed * verb = w~take~upper -- TEXT (w~peek: without taking it) * into = w~takeInto -- "x": "INTO name", anywhere, or .nil * v = w~takeValue(context) -- the next word, or with "FROM name" * -- that variable's value (.nil: unset; * -- context: a .CommandContext) * w~items, w[i], w~quoted(i), w~isKeyword(i, "FROM"), w~rest (an Array) * * .CommandIO: what both write and read. Without WITH OUTPUT or WITH ERROR, * the lines go to the console, as a command's own output would. * * .CommandIO~output(context, lines) a string or a collection of lines * .CommandIO~error(context, lines) the same, for WITH ERROR * .CommandIO~lines(context) the WITH INPUT lines (an Array) */ ::class CommandWords public ::attribute error get -- .nil, or why the command cannot be split ::method init expose words quoted error use strict arg command words = .array~new; quoted = .array~new; error = .nil command = command~string i = 1; n = command~length do forever i = command~verify(" " || "09"x, , i) -- the next word's start if i = 0 then leave q = command~subchar(i) if q == '"' | q == "'" then do start = i; word = ""; j = i + 1 do forever k = command~pos(q, j) if k = 0 then do error = "a quote" q "is not closed:" command~substr(i) return end word ||= command~substr(j, k - j) if command~subchar(k + 1) == q then do -- a doubled quote word ||= q; j = k + 2; iterate end leave end i = k + 1 if i <= n & (" " || "09"x)~pos(command~subchar(i)) = 0 then do error = "a quoted word must end at a blank:" command~substr(start) return end words~append(word); quoted~append(.true) end else do k = command~verify(" " || "09"x, "M", i) if k = 0 then k = n + 1 words~append(command~substr(i, k - i)); quoted~append(.false) i = k end end ::method items expose words return words~items ::method "[]" expose words use strict arg i return words[i] ::method quoted expose quoted use strict arg i return quoted[i] == .true -- word i is this keyword (unquoted, any case) ::method isKeyword expose words quoted use strict arg i, keyword if i < 1 | i > words~items then return .false return \quoted[i] & words[i]~upper == keyword~upper -- the first word, taken out (.nil when there is none) ::method take expose words quoted if words~items = 0 then return .nil quoted~delete(1) return words~delete(1) ::method peek expose words if words~items = 0 then return .nil return words[1] -- "INTO name", anywhere: the name, both words taken out; else .nil (only -- the first one: a second "INTO name" stays, for the handler to refuse) ::method takeInto expose words quoted do i = 1 to words~items - 1 if \self~isKeyword(i, "INTO") then iterate if quoted[i + 1] then iterate name = words[i + 1] words~delete(i + 1); quoted~delete(i + 1); words~delete(i); quoted~delete(i) return name end return .nil -- the next value: a word, or with "FROM name" that variable's value (.nil -- when it has none); .nil when there are no words left ::method takeValue expose words use strict arg context if self~isKeyword(1, "FROM") & words~items >= 2 & \self~quoted(2) then do self~take name = self~take if \context~hasVar(name) then return .nil return context[name] end return self~take -- the words left, an Array ::method rest expose words return words~copy ::class CommandIO public ::method output class use strict arg context, lines if context~isRedirected("Output") then out = context~output else out = .output self~write(out, lines) ::method error class use strict arg context, lines if context~isRedirected("Error") then out = context~error else out = .error self~write(out, lines) ::method write class private use strict arg out, lines if lines~isA(.String) then out~lineout(lines) else do line over lines~makeArray out~lineout(line~string) end ::method lines class use strict arg context if \context~isRedirected("Input") then return .array~new in = context~input lines = .array~new do while in~lines > 0 lines~append(in~linein) end return lines