MCPcopy Create free account
hub / github.com/emscripten-core/emscripten / parse_value

Function parse_value

tools/cmdline.py:649–736  ·  view source on GitHub ↗
(text, expected_type)

Source from the content-addressed store, hash-verified

647
648
649def parse_value(text, expected_type):
650 # Note that using response files can introduce whitespace, if the file
651 # has a newline at the end. For that reason, we rstrip() in relevant
652 # places here.
653 def parse_string_value(text):
654 first = text[0]
655 if first in {"'", '"'}:
656 text = text.rstrip()
657 if text[-1] != text[0] or len(text) < 2:
658 raise ValueError(f'unclosed quoted string. expected final character to be "{text[0]}" and length to be greater than 1 in "{text[0]}"')
659 return text[1:-1]
660 return text
661
662 def parse_string_list_members(text):
663 sep = ','
664 values = text.split(sep)
665 result = []
666 index = 0
667 while True:
668 current = values[index].lstrip() # Cannot safely rstrip for cases like: "HERE-> ,"
669 if not len(current):
670 raise ValueError('empty value in string list')
671 first = current[0]
672 if first not in {"'", '"'}:
673 result.append(current.rstrip())
674 else:
675 start = index
676 while True: # Continue until closing quote found
677 if index >= len(values):
678 raise ValueError(f"unclosed quoted string. expected final character to be '{first}' in '{values[start]}'")
679 new = values[index].rstrip()
680 if new and new[-1] == first:
681 if start == index:
682 result.append(current.rstrip()[1:-1])
683 else:
684 result.append((current + sep + new)[1:-1])
685 break
686 else:
687 current += sep + values[index]
688 index += 1
689
690 index += 1
691 if index >= len(values):
692 break
693 return result
694
695 def parse_string_list(text):
696 text = text.rstrip()
697 if text and text[0] == '[':
698 if text[-1] != ']':
699 raise ValueError('unterminated string list. expected final character to be "]"')
700 text = text[1:-1]
701 if not text.strip():
702 return []
703 return parse_string_list_members(text)
704
705 if expected_type == list or (text and text[0] == '['):
706 # if json parsing fails, we fall back to our own parser, which can handle a few

Callers 1

apply_user_settingsFunction · 0.85

Calls 3

parse_string_listFunction · 0.85
parse_string_valueFunction · 0.85
typeEnum · 0.50

Tested by

no test coverage detected