Created
July 18, 2018 01:44
-
-
Save stillwwater/dc16c7422cdb036186ccfb54e5e93711 to your computer and use it in GitHub Desktop.
Converts .var files to INI, YAML, or .var with stripped comments.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
Converts .var files to INI, YAML, or .var with stripped comments. | |
input: | |
""" | |
This string documents | |
the file. | |
""" | |
:name | |
key value # this is a comment | |
num 88 | |
output (INI): | |
[name] | |
key=value | |
num=88 | |
output (YAML): | |
name: | |
key: value | |
num: 88 | |
output: | |
:name | |
key value | |
num 88 | |
} | |
program var2ini; | |
{$MACRO ON} | |
{$define INI} | |
{$define COMMENT := '#'} | |
{$define DOC_STRING := '"""'} | |
{$define SYMBOL := ':'} | |
{$define KEY_VALUE_SEPARATOR := ' '} | |
{$define C_SYMBOL_START := ':'} | |
{$define C_SYMBOL_END := ''} | |
{$define C_KEY_VALUE_SEPARATOR := ' '} | |
{$define C_INDENTATION := ''} | |
{$ifdef INI} | |
{$define C_SYMBOL_START := '['} | |
{$define C_SYMBOL_END := ']'} | |
{$define C_KEY_VALUE_SEPARATOR := '='} | |
{$endif} | |
{$ifdef YAML} | |
{$define C_SYMBOL_START := ''} | |
{$define C_SYMBOL_END := ':'} | |
{$define C_KEY_VALUE_SEPARATOR := ': '} | |
{$define C_INDENTATION := ' '} | |
{$endif} | |
procedure error(const message: string; const line_number: integer; const src: string); | |
begin | |
writeln; | |
writeln('error: ', message, ' at ', line_number); | |
if src <> '' then | |
writeln(' -> ', src); | |
writeln; | |
writeln('aborting...'); | |
halt; | |
end; | |
function trim(const str: string): string; | |
var | |
start_pos : integer; | |
end_pos : integer; | |
begin | |
for start_pos := 1 to length(str) do | |
if str[start_pos] <> ' ' then break; | |
for end_pos := length(str) downto 1 do | |
if str[end_pos] <> ' ' then break; | |
trim := copy(str, start_pos, end_pos); | |
end; | |
procedure strip(var str: string; const match: string); | |
var | |
char_pos: integer; | |
begin | |
char_pos := pos(match, str); | |
if char_pos > 0 then | |
str := copy(str, 1, char_pos - 1); | |
end; | |
procedure compile_symbol(var target: text; const line: string; const line_pos: integer); | |
begin | |
writeln(target, C_SYMBOL_START, copy(line, 2, length(line) - 1), C_SYMBOL_END); // Identifier | |
end; | |
procedure compile_pair(var target: text; const line: string; const line_pos: integer); | |
var | |
char_pos: integer; | |
begin | |
char_pos := pos(KEY_VALUE_SEPARATOR, line); | |
if char_pos = 0 then | |
error('error: key with no value, aborting...', line_pos, line); | |
write(target, C_INDENTATION, copy(line, 1, char_pos - 1), C_KEY_VALUE_SEPARATOR); // Key | |
writeln(target, trim(copy(line, char_pos, length(line) - char_pos + 1))); // Value | |
end; | |
procedure compile(const source_name: string; const target_name: string); | |
var | |
source : text; | |
target : text; | |
line_pos : integer; | |
multiline : boolean; | |
line : string; | |
begin | |
assign(source, source_name); | |
assign(target, target_name); | |
reset(source); | |
rewrite(target); | |
line_pos := 0; | |
multiline := false; | |
repeat | |
line_pos := line_pos + 1; | |
readln(source, line); | |
strip(line, COMMENT); | |
line := trim(line); | |
if line = DOC_STRING then | |
begin | |
multiline := not multiline; | |
continue; | |
end; | |
if (line = '') or (multiline) then | |
continue; | |
if line[1] = SYMBOL then | |
begin | |
compile_symbol(target, line, line_pos); | |
continue; | |
end; | |
compile_pair(target, line, line_pos); | |
until eof(source); | |
close(source); | |
close(target); | |
end; | |
var | |
input : string; | |
output : string; | |
begin | |
input := paramstr(1); | |
output := paramstr(2); | |
compile(input, output); | |
writeln('OK: output to ', output); | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment