2014-08-28 13:47:58 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# Copyright 2014 by Sam Mikes. All rights reserved.
|
|
|
|
# This code is governed by the BSD license found in the LICENSE file.
|
|
|
|
|
|
|
|
# This code provides a fallback parser that can handle the subset of
|
|
|
|
# YAML used in test262 frontmatter
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
mYamlKV = re.compile(r"(.*?):(.*)")
|
|
|
|
mYamlIntDigits = re.compile(r"^[-0-9]*$")
|
|
|
|
mYamlFloatDigits = re.compile(r"^[-.0-9eE]*$")
|
|
|
|
mYamlListPattern = re.compile(r"^\[(.*)\]$")
|
|
|
|
mYamlMultilineList = re.compile(r"^ *- (.*)$")
|
|
|
|
|
|
|
|
def load(str):
|
2016-01-31 19:16:12 +01:00
|
|
|
return myReadDict(str.splitlines())[1]
|
|
|
|
|
|
|
|
def myReadDict(lines, indent=""):
|
2014-08-28 13:47:58 +02:00
|
|
|
dict = None
|
2015-07-01 16:19:33 +02:00
|
|
|
key = None
|
|
|
|
emptyLines = 0
|
2014-08-28 13:47:58 +02:00
|
|
|
while lines:
|
2016-01-31 19:16:12 +01:00
|
|
|
if not lines[0].startswith(indent):
|
|
|
|
break
|
|
|
|
|
2014-08-28 13:47:58 +02:00
|
|
|
line = lines.pop(0)
|
|
|
|
if myIsAllSpaces(line):
|
2015-07-01 16:19:33 +02:00
|
|
|
emptyLines += 1
|
2014-08-28 13:47:58 +02:00
|
|
|
continue
|
2014-12-01 21:46:34 +01:00
|
|
|
result = mYamlKV.match(line)
|
2015-07-01 16:19:33 +02:00
|
|
|
|
2014-08-28 13:47:58 +02:00
|
|
|
if result:
|
|
|
|
if not dict:
|
|
|
|
dict = {}
|
|
|
|
key = result.group(1).strip()
|
|
|
|
value = result.group(2).strip()
|
2016-01-31 19:16:12 +01:00
|
|
|
(lines, value) = myReadValue(lines, value, indent)
|
2014-08-28 13:47:58 +02:00
|
|
|
dict[key] = value
|
|
|
|
else:
|
2015-07-01 16:19:33 +02:00
|
|
|
if dict and key and key in dict:
|
|
|
|
c = " " if emptyLines == 0 else "\n" * emptyLines
|
|
|
|
dict[key] += c + line.strip()
|
|
|
|
else:
|
|
|
|
raise Exception("monkeyYaml is confused at " + line)
|
|
|
|
emptyLines = 0
|
2016-01-31 19:16:12 +01:00
|
|
|
return lines, dict
|
2014-08-28 13:47:58 +02:00
|
|
|
|
2016-01-31 19:16:12 +01:00
|
|
|
def myReadValue(lines, value, indent):
|
2016-06-01 00:04:31 +02:00
|
|
|
if value == ">" or value == "|":
|
2014-08-28 13:47:58 +02:00
|
|
|
(lines, value) = myMultiline(lines, value)
|
|
|
|
value = value + "\n"
|
|
|
|
return (lines, value)
|
2016-01-31 19:16:12 +01:00
|
|
|
if lines and not value:
|
|
|
|
if myMaybeList(lines[0]):
|
|
|
|
return myMultilineList(lines, value)
|
|
|
|
indentMatch = re.match("(" + indent + r"\s+)", lines[0])
|
|
|
|
if indentMatch:
|
|
|
|
return myReadDict(lines, indentMatch.group(1))
|
|
|
|
return lines, myReadOneLine(value)
|
2014-08-28 13:47:58 +02:00
|
|
|
|
|
|
|
def myMaybeList(value):
|
|
|
|
return mYamlMultilineList.match(value)
|
|
|
|
|
|
|
|
def myMultilineList(lines, value):
|
|
|
|
# assume no explcit indentor (otherwise have to parse value)
|
|
|
|
value = []
|
|
|
|
indent = None
|
|
|
|
while lines:
|
|
|
|
line = lines.pop(0)
|
|
|
|
leading = myLeadingSpaces(line)
|
|
|
|
if myIsAllSpaces(line):
|
|
|
|
pass
|
2018-04-24 18:52:57 +02:00
|
|
|
elif indent is not None and leading < indent:
|
2014-08-28 13:47:58 +02:00
|
|
|
lines.insert(0, line)
|
|
|
|
break;
|
|
|
|
else:
|
|
|
|
indent = indent or leading
|
|
|
|
value += [myReadOneLine(myRemoveListHeader(indent, line))]
|
|
|
|
return (lines, value)
|
2014-12-01 21:46:34 +01:00
|
|
|
|
2014-08-28 13:47:58 +02:00
|
|
|
def myRemoveListHeader(indent, line):
|
|
|
|
line = line[indent:]
|
|
|
|
return mYamlMultilineList.match(line).group(1)
|
|
|
|
|
|
|
|
def myReadOneLine(value):
|
|
|
|
if mYamlListPattern.match(value):
|
|
|
|
return myFlowList(value)
|
|
|
|
elif mYamlIntDigits.match(value):
|
|
|
|
try:
|
|
|
|
value = int(value)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
elif mYamlFloatDigits.match(value):
|
|
|
|
try:
|
|
|
|
value = float(value)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
return value
|
2014-12-01 21:46:34 +01:00
|
|
|
|
2014-08-28 13:47:58 +02:00
|
|
|
def myFlowList(value):
|
|
|
|
result = mYamlListPattern.match(value)
|
|
|
|
values = result.group(1).split(",")
|
|
|
|
return [myReadOneLine(v.strip()) for v in values]
|
|
|
|
|
|
|
|
def myMultiline(lines, value):
|
|
|
|
# assume no explcit indentor (otherwise have to parse value)
|
|
|
|
value = []
|
2015-03-30 22:46:49 +02:00
|
|
|
indent = myLeadingSpaces(lines[0])
|
2014-08-28 13:47:58 +02:00
|
|
|
while lines:
|
|
|
|
line = lines.pop(0)
|
|
|
|
if myIsAllSpaces(line):
|
|
|
|
value += ["\n"]
|
|
|
|
elif myLeadingSpaces(line) < indent:
|
|
|
|
lines.insert(0, line)
|
|
|
|
break;
|
|
|
|
else:
|
|
|
|
value += [line[(indent):]]
|
|
|
|
value = " ".join(value)
|
|
|
|
return (lines, value)
|
|
|
|
|
|
|
|
def myIsAllSpaces(line):
|
|
|
|
return len(line.strip()) == 0
|
|
|
|
|
|
|
|
def myLeadingSpaces(line):
|
|
|
|
return len(line) - len(line.lstrip(' '))
|