Skip to content

Commit

Permalink
tools-interface: remove code that duplicates file-source-records
Browse files Browse the repository at this point in the history
Note that the duplicated (now removed) code, particularly
read-file-header-component, stopped at the first blank line, just as
the function by the same name in file-source-records does, even though project
files don't (I think?) have a non-header part. Potentially we could add a
skip-blank-lines? option to read-file-header. But in any case, I believe the
new code is consistent with the old code.
  • Loading branch information
cgay committed Nov 6, 2023
1 parent d24b73f commit ef51543
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 132 deletions.
1 change: 1 addition & 0 deletions sources/corba/scepter/tool/tool-scepter-library.dylan
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ define library tool-scepter
use dylan;
use common-dylan;
use collections;
use file-source-records;
use system;
use io;
use tools-interface;
Expand Down
1 change: 1 addition & 0 deletions sources/corba/scepter/tool/tool-scepter-module.dylan
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Warranty: Distributed WITHOUT WARRANTY OF ANY KIND

define module tool-scepter
use common-dylan;
use file-source-records;
use format;
use streams;
use table-extensions;
Expand Down
2 changes: 1 addition & 1 deletion sources/corba/scepter/tool/tool-scepter.dylan
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ end method;

define inline-only function read-spec-file (file :: <file-locator>)
=> (specification)
read-keyword-pair-file(file);
read-file-header(file);
end function;

define function date-as-string (date :: false-or(<date>)) => (r :: <string>)
Expand Down
2 changes: 1 addition & 1 deletion sources/lib/motley/tool-iface.dylan
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ define function motley-invoke
debug-message("Motley invoked. Spec: %=, project: %=, last: %=, clean: %=\n",
spec-file, project-file, last-run & date-as-string(last-run),
clean-build?);
let spec-keys = read-keyword-pair-file(spec-file);
let spec-keys = read-file-header(spec-file);
motley-process-spec(spec-keys, spec-file, project-file, last-run,
clean-build?: clean-build?);
end function motley-invoke;
Expand Down
1 change: 1 addition & 0 deletions sources/lib/parser-generator/tool/library.dylan
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Warranty: Distributed WITHOUT WARRANTY OF ANY KIND
define library tool-parser-generator
use dylan;
use common-dylan;
use file-source-records;
use io;
use system;
use tools-interface;
Expand Down
1 change: 1 addition & 0 deletions sources/lib/parser-generator/tool/module.dylan
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ define module tool-parser-generator
use common-dylan;
use simple-debugging, import: { debug-out };
use date;
use file-source-records;
use file-system;
use format;
use streams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ define method parser-generator-invoke

let keyval = keyword-file-element-value;
let keyline = keyword-file-element-line;
let spec = read-keyword-pair-file(spec-file);
let spec = read-file-header(spec-file);
local method key (sym :: <symbol>) element(spec, sym, default: #()) end;
local method single (sym :: <symbol>, #key default = $unsupplied)
if (sym.key.size = 1)
Expand Down
26 changes: 14 additions & 12 deletions sources/lib/source-records/header-reader.dylan
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ define class <bad-header> (<simple-error>)
end;

define method read-file-header (file :: <locator>)
=> (keys :: <table>, lines :: <integer>, chars :: <integer>)
=> (keys :: <table>, nlines :: <integer>, nchars :: <integer>)
block ()
with-open-file (stream = file)
read-header-from-stream(stream)
Expand All @@ -27,6 +27,12 @@ end method;

define constant $unique-header-keywords = #(#"module", #"language");

// Read 'key: val' pairs up through the empty line that separates the header
// from the main source code. Returns
// * a table mapping key (interned as a symbol) to values which are either a
// single string or, if there were continuation lines, a sequence of strings.
// * the number of lines read
// * the number of characters read
define method read-header-from-stream (stream :: <stream>)
=> (keys :: <table>, lines :: <integer>, chars :: <integer>)
let keys = make(<table>);
Expand All @@ -49,6 +55,7 @@ define method read-header-from-stream (stream :: <stream>)
end
end method;

// Read one 'key: val' pair, with val possibly having continuation lines.
define method read-file-header-component (stream :: <stream>)
=> (key, strings, nlines, end-of-header?)
let (key-line, nl?) = read-line(stream, on-end-of-stream: "");
Expand All @@ -65,8 +72,9 @@ define method read-file-header-component (stream :: <stream>)
if (header-end-marker-line?(continuation-line))
values(key, reverse!(text-strings), nlines, #t)
else
let text = parse-header-continuation-line(continuation-line);
loop(pair(text, text-strings), nlines)
loop(pair(strip(continuation-line, test: header-whitespace?),
text-strings),
nlines)
end
else
char & unread-element(stream, char);
Expand All @@ -77,11 +85,9 @@ define method read-file-header-component (stream :: <stream>)
end method;

define method parse-header-keyword-line (line :: <string>)
let colon = position(line, ':');
if (~colon)
signal(make(<bad-header>,
message: format-to-string("Syntax error on line: %=", line)))
end;
let colon = position(line, ':')
| signal(make(<bad-header>,
message: format-to-string("Syntax error on line: %=", line)));
values(as(<symbol>, copy-sequence(line, end: colon)),
strip(line, start: colon + 1, test: header-whitespace?))
end method;
Expand All @@ -91,10 +97,6 @@ define inline function header-whitespace? (c :: <character>) => (white? :: <bool
c == ' ' | c == '\t'
end;

define method parse-header-continuation-line (line :: <string>)
strip(line, test: header-whitespace?)
end method;

define function header-end-marker-line? (line :: <string>)
every?(header-whitespace?, line)
end function;
5 changes: 3 additions & 2 deletions sources/project-manager/tools-interface/library.dylan
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ Warranty: Distributed WITHOUT WARRANTY OF ANY KIND
define library tools-interface
use dylan;
use common-dylan;
use system;
use file-source-records;
use io;
use system;

export tools-interface;
end library tools-interface;
Expand All @@ -21,6 +22,7 @@ define module tools-interface
use dylan;
use common-extensions;
use machine-words;
use file-source-records;
use format;
use format-out;
use locators;
Expand Down Expand Up @@ -59,7 +61,6 @@ define module tools-interface
project-information-base-address-setter,
project-information-remaining-keys-setter;

export read-keyword-pair-file, read-keyword-pair-stream;
export write-keyword-pair-file, write-keyword-pair-stream;
export <keyword-file-element>,
keyword-file-element-value, keyword-file-element-value-setter,
Expand Down
116 changes: 2 additions & 114 deletions sources/project-manager/tools-interface/project-files.dylan
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ end function read-project-file;
// Read a project file from a stream.

define function read-project-from-stream
(s :: <stream>) => (p :: <project-information>, version :: <integer>)
let t :: <table> = read-keyword-pair-stream(s, 1);
(stream :: <stream>) => (p :: <project-information>, version :: <integer>)
let t :: <table> = read-header-from-stream(stream);
let error? :: <boolean> = #f;
let keyval = keyword-file-element-value;
let keyline = keyword-file-element-line;
Expand Down Expand Up @@ -303,115 +303,3 @@ define function write-keyword-pair-stream (s :: <stream>, keys :: <table>) => ()
end if;
end for;
end function write-keyword-pair-stream;


define function read-keyword-pair-file
(file :: type-union(<string>, <locator>))
=> (keys :: <table>)
let handler <tool-warning-condition> = tool-warning-add-file-handler(file);
with-open-file (stream = file, direction: #"input")
read-keyword-pair-stream(stream, 1)
end
end function read-keyword-pair-file;


define function read-keyword-pair-stream
(s :: <stream>, first-line-no :: <integer>)
=> (keys :: <table>, last-line-no :: <integer>)
let keys = make(<table>);
local
method loop (line-no :: <integer>) => (keys, last-line-no :: <integer>)
let (key, strings, eoh?, line-no)
= read-file-header-component(s, line-no);
if (key)
let old-strings = element(keys, key, default: #());
keys[key] := concatenate!(old-strings, strings);
end if;
if (eoh?)
values(keys, line-no)
else
loop(line-no)
end if
end method;
loop(first-line-no)
end function read-keyword-pair-stream;


define function read-file-header-component
(s :: <stream>, line-no :: <integer>)
=> (key, strings, eoh?, new-line-no)
let (key-line, nl?) = read-line(s, on-end-of-stream: "");
if (header-end-marker-line?(key-line))
values(#f, #f, #t, line-no)
else
local
method loop (text-strings :: <list>)
line-no := line-no + 1;
let char = read-element(s, on-end-of-stream: #f);
if (char == ' ' | char == '\t')
let (continuation-line, nl?) = read-line(s, on-end-of-stream: "");
if (header-end-marker-line?(continuation-line))
values(text-strings, #t, line-no)
else
let text = parse-header-continuation-line(continuation-line, line-no);
loop(pair(text, text-strings))
end;
else
if (char) unread-element(s, char) end;
values(text-strings, #f, line-no)
end;
end method;
let (key, text) = parse-header-keyword-line(key-line, line-no);
let (text-strings, past-eoh?) = loop(list(text));
values(key, reverse!(text-strings), past-eoh?, line-no)
end;
end function read-file-header-component;


define function parse-header-keyword-line
(line :: <string>, line-no :: <integer>)
let colon = position(line, ':');
if (~colon)
tool-error("syntax error", line: line-no);
end;
values(as(<symbol>, copy-sequence(line, end: colon)),
make(<keyword-file-element>,
value: trim-whitespace(line, colon + 1),
line: line-no))
end function parse-header-keyword-line;

define function parse-header-continuation-line
(line :: <string>, line-no :: <integer>)
make(<keyword-file-element>,
value: trim-whitespace(line, 0),
line: line-no)
end function parse-header-continuation-line;

define function header-end-marker-line? (line :: <string>)
every?(method (c) c == ' ' | c == '\t' end, line)
end function;

define function trim-whitespace (line :: <string>, start)
local method bwd (line, start, len)
let last = len - 1;
let c = line[last];
if (c == ' ' | c == '\t')
bwd(line, start, last)
else
copy-sequence(line, start: start, end: len)
end;
end method;
local method fwd (line, start, len)
if (start == len)
""
else
let c = line[start];
if (c == ' ' | c == '\t')
fwd(line, start + 1, len)
else
bwd(line, start, len)
end;
end;
end method;
fwd(line, start, size(line))
end function;
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ define function tool-name-from-specification
//---*** andrewa: bootstrapping nastiness, only the first should be necessary
if (extsym == #"spec" | extsym == #".spec")
// Read the header of the file:
let h = read-keyword-pair-file(spec-file);
let h = read-file-header(spec-file);
let tool = element(h, origin:, default: #());
if (tool.size ~= 1)
tool-error("specification file must contain a single \"origin:\" "
Expand Down

0 comments on commit ef51543

Please sign in to comment.