new syntax for internals
This commit is contained in:
96
toml.cm
96
toml.cm
@@ -26,23 +26,33 @@ function parse_toml(toml_text) {
|
||||
var current_section = result
|
||||
var current_section_name = ''
|
||||
|
||||
for (var i = 0; i < length(lines); i++) {
|
||||
var line = trim(lines[i])
|
||||
var i = 0
|
||||
var line = null
|
||||
var inner = null
|
||||
var section_path = null
|
||||
var j = 0
|
||||
var key = null
|
||||
var eq_index = null
|
||||
var key_part = null
|
||||
var value = null
|
||||
var unquoted = null
|
||||
for (i = 0; i < length(lines); i++) {
|
||||
line = trim(lines[i])
|
||||
if (line == null) line = lines[i]
|
||||
// Skip empty lines and comments
|
||||
if (!line || starts_with(line, '#')) continue
|
||||
|
||||
// Section header
|
||||
if (starts_with(line, '[') && ends_with(line, ']')) {
|
||||
var inner = text(line, 1, -1)
|
||||
var section_path = parse_key_path(inner)
|
||||
inner = text(line, 1, -1)
|
||||
section_path = parse_key_path(inner)
|
||||
if (section_path == null) return null
|
||||
|
||||
current_section = result
|
||||
current_section_name = text(section_path, '.')
|
||||
|
||||
for (var j = 0; j < length(section_path); j++) {
|
||||
var key = section_path[j]
|
||||
for (j = 0; j < length(section_path); j++) {
|
||||
key = section_path[j]
|
||||
|
||||
// Only treat null as "missing"; do not clobber false/0/""
|
||||
if (current_section[key] == null) {
|
||||
@@ -58,18 +68,18 @@ function parse_toml(toml_text) {
|
||||
}
|
||||
|
||||
// Key-value pair
|
||||
var eq_index = search(line, '=')
|
||||
eq_index = search(line, '=')
|
||||
if (eq_index != null && eq_index > 0) {
|
||||
var key_part = trim(text(line, 0, eq_index))
|
||||
var value = trim(text(line, eq_index + 1))
|
||||
key_part = trim(text(line, 0, eq_index))
|
||||
value = trim(text(line, eq_index + 1))
|
||||
if (key_part == null) key_part = trim(text(line, 0, eq_index))
|
||||
if (value == null) value = trim(text(line, eq_index + 1))
|
||||
|
||||
var key = parse_key(key_part)
|
||||
key = parse_key(key_part)
|
||||
if (key == null) return null
|
||||
|
||||
if (starts_with(value, '"') && ends_with(value, '"')) {
|
||||
var unquoted = text(value, 1, -1)
|
||||
unquoted = text(value, 1, -1)
|
||||
current_section[key] = toml_unescape(unquoted)
|
||||
if (current_section[key] == null) return null
|
||||
} else if (starts_with(value, '[') && ends_with(value, ']')) {
|
||||
@@ -91,9 +101,9 @@ function parse_toml(toml_text) {
|
||||
|
||||
function parse_key(str) {
|
||||
if (!is_text(str)) return null
|
||||
|
||||
var inner = null
|
||||
if (starts_with(str, '"') && ends_with(str, '"')) {
|
||||
var inner = text(str, 1, -1)
|
||||
inner = text(str, 1, -1)
|
||||
return toml_unescape(inner)
|
||||
}
|
||||
return str
|
||||
@@ -107,12 +117,15 @@ function parse_key_path(str) {
|
||||
var current = ''
|
||||
var in_quote = false
|
||||
|
||||
for (var i = 0; i < length(str); i++) {
|
||||
var c = str[i]
|
||||
var i = 0
|
||||
var c = null
|
||||
var piece = null
|
||||
for (i = 0; i < length(str); i++) {
|
||||
c = str[i]
|
||||
if (c == '"' && (i == 0 || str[i - 1] != '\\')) {
|
||||
in_quote = !in_quote
|
||||
} else if (c == '.' && !in_quote) {
|
||||
var piece = trim(current)
|
||||
piece = trim(current)
|
||||
if (piece == null) piece = trim(current)
|
||||
push(parts, parse_key(piece))
|
||||
current = ''
|
||||
@@ -140,14 +153,17 @@ function parse_array(str) {
|
||||
var current = ''
|
||||
var in_quotes = false
|
||||
|
||||
for (var i = 0; i < length(s); i++) {
|
||||
var ch = s[i]
|
||||
var i = 0
|
||||
var ch = null
|
||||
var piece = null
|
||||
for (i = 0; i < length(s); i++) {
|
||||
ch = s[i]
|
||||
|
||||
if (ch == '"' && (i == 0 || s[i - 1] != '\\')) {
|
||||
in_quotes = !in_quotes
|
||||
current = current + ch
|
||||
} else if (ch == ',' && !in_quotes) {
|
||||
var piece = trim(current)
|
||||
piece = trim(current)
|
||||
if (piece == null) piece = trim(current)
|
||||
push(items, parse_value(piece))
|
||||
current = ''
|
||||
@@ -181,12 +197,14 @@ function encode_toml(obj) {
|
||||
var result = []
|
||||
|
||||
function encode_value(value) {
|
||||
var items = null
|
||||
var i = 0
|
||||
if (is_text(value)) return '"' + toml_escape(value) + '"'
|
||||
if (is_logical(value)) return value ? 'true' : 'false'
|
||||
if (is_number(value)) return text(value)
|
||||
if (is_array(value)) {
|
||||
var items = []
|
||||
for (var i = 0; i < length(value); i++) push(items, encode_value(value[i]))
|
||||
items = []
|
||||
for (i = 0; i < length(value); i++) push(items, encode_value(value[i]))
|
||||
return '[' + text(items, ', ') + ']'
|
||||
}
|
||||
return text(value)
|
||||
@@ -201,29 +219,41 @@ function encode_toml(obj) {
|
||||
|
||||
// First pass: encode top-level simple values
|
||||
var keys = array(obj)
|
||||
for (var i = 0; i < length(keys); i++) {
|
||||
var key = keys[i]
|
||||
var value = obj[key]
|
||||
var i = 0
|
||||
var key = null
|
||||
var value = null
|
||||
for (i = 0; i < length(keys); i++) {
|
||||
key = keys[i]
|
||||
value = obj[key]
|
||||
if (!is_object(value)) push(result, quote_key(key) + ' = ' + encode_value(value))
|
||||
}
|
||||
|
||||
// Second pass: encode nested objects
|
||||
function encode_section(o, path) {
|
||||
var keys = array(o)
|
||||
for (var i = 0; i < length(keys); i++) {
|
||||
var key = keys[i]
|
||||
var value = o[key]
|
||||
var i = 0
|
||||
var key = null
|
||||
var value = null
|
||||
var quoted = null
|
||||
var section_path = null
|
||||
var section_keys = null
|
||||
var j = 0
|
||||
var sk = null
|
||||
var sv = null
|
||||
for (i = 0; i < length(keys); i++) {
|
||||
key = keys[i]
|
||||
value = o[key]
|
||||
|
||||
if (is_object(value)) {
|
||||
var quoted = quote_key(key)
|
||||
var section_path = path ? path + '.' + quoted : quoted
|
||||
quoted = quote_key(key)
|
||||
section_path = path ? path + '.' + quoted : quoted
|
||||
push(result, '[' + section_path + ']')
|
||||
|
||||
// Direct properties
|
||||
var section_keys = array(value)
|
||||
for (var j = 0; j < length(section_keys); j++) {
|
||||
var sk = section_keys[j]
|
||||
var sv = value[sk]
|
||||
section_keys = array(value)
|
||||
for (j = 0; j < length(section_keys); j++) {
|
||||
sk = section_keys[j]
|
||||
sv = value[sk]
|
||||
if (!is_object(sv)) push(result, quote_key(sk) + ' = ' + encode_value(sv))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user