2018-04-17 22:57:45 -04:00
|
|
|
#
|
2019-06-06 04:55:46 -04:00
|
|
|
# SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2018-04-17 22:57:45 -04:00
|
|
|
#
|
2021-01-25 21:49:01 -05:00
|
|
|
import abc
|
2018-04-17 22:57:45 -04:00
|
|
|
import os
|
2019-04-02 04:40:43 -04:00
|
|
|
import re
|
2019-03-19 02:35:47 -04:00
|
|
|
from collections import namedtuple
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
from pyparsing import (Combine, Forward, Group, Literal, OneOrMore, Optional, ParseFatalException, Suppress, Word,
|
|
|
|
ZeroOrMore, alphanums, alphas, indentedBlock, originalTextFor, restOfLine)
|
|
|
|
from sdkconfig import SDKConfig
|
2019-03-19 02:35:47 -04:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
KeyGrammar = namedtuple('KeyGrammar', 'grammar min max required')
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2018-12-04 07:46:48 -05:00
|
|
|
|
2019-03-19 02:35:47 -04:00
|
|
|
class FragmentFile():
|
2018-12-04 07:46:48 -05:00
|
|
|
"""
|
|
|
|
Fragment file internal representation. Parses and stores instances of the fragment definitions
|
|
|
|
contained within the file.
|
|
|
|
"""
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2019-03-19 02:35:47 -04:00
|
|
|
def __init__(self, fragment_file, sdkconfig):
|
|
|
|
try:
|
2021-01-25 21:49:01 -05:00
|
|
|
fragment_file = open(fragment_file, 'r')
|
2019-03-19 02:35:47 -04:00
|
|
|
except TypeError:
|
|
|
|
pass
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2019-03-19 02:35:47 -04:00
|
|
|
path = os.path.realpath(fragment_file.name)
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2019-03-19 02:35:47 -04:00
|
|
|
indent_stack = [1]
|
|
|
|
|
|
|
|
class parse_ctx:
|
|
|
|
fragment = None # current fragment
|
2021-01-25 21:49:01 -05:00
|
|
|
key = '' # current key
|
2019-03-19 02:35:47 -04:00
|
|
|
keys = list() # list of keys parsed
|
|
|
|
key_grammar = None # current key grammar
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def reset():
|
|
|
|
parse_ctx.fragment_instance = None
|
2021-01-25 21:49:01 -05:00
|
|
|
parse_ctx.key = ''
|
2019-03-19 02:35:47 -04:00
|
|
|
parse_ctx.keys = list()
|
|
|
|
parse_ctx.key_grammar = None
|
|
|
|
|
|
|
|
def fragment_type_parse_action(toks):
|
|
|
|
parse_ctx.reset()
|
|
|
|
parse_ctx.fragment = FRAGMENT_TYPES[toks[0]]() # create instance of the fragment
|
|
|
|
return None
|
|
|
|
|
|
|
|
def expand_conditionals(toks, stmts):
|
|
|
|
try:
|
2021-01-25 21:49:01 -05:00
|
|
|
stmt = toks['value']
|
2019-03-19 02:35:47 -04:00
|
|
|
stmts.append(stmt)
|
|
|
|
except KeyError:
|
|
|
|
try:
|
2021-01-25 21:49:01 -05:00
|
|
|
conditions = toks['conditional']
|
2019-03-19 02:35:47 -04:00
|
|
|
for condition in conditions:
|
|
|
|
try:
|
|
|
|
_toks = condition[1]
|
|
|
|
_cond = condition[0]
|
|
|
|
if sdkconfig.evaluate_expression(_cond):
|
|
|
|
expand_conditionals(_toks, stmts)
|
|
|
|
break
|
|
|
|
except IndexError:
|
|
|
|
expand_conditionals(condition[0], stmts)
|
|
|
|
except KeyError:
|
|
|
|
for tok in toks:
|
|
|
|
expand_conditionals(tok, stmts)
|
|
|
|
|
|
|
|
def key_body_parsed(pstr, loc, toks):
|
|
|
|
stmts = list()
|
|
|
|
expand_conditionals(toks, stmts)
|
|
|
|
|
|
|
|
if parse_ctx.key_grammar.min and len(stmts) < parse_ctx.key_grammar.min:
|
|
|
|
raise ParseFatalException(pstr, loc, "fragment requires at least %d values for key '%s'" %
|
|
|
|
(parse_ctx.key_grammar.min, parse_ctx.key))
|
|
|
|
|
|
|
|
if parse_ctx.key_grammar.max and len(stmts) > parse_ctx.key_grammar.max:
|
|
|
|
raise ParseFatalException(pstr, loc, "fragment requires at most %d values for key '%s'" %
|
|
|
|
(parse_ctx.key_grammar.max, parse_ctx.key))
|
|
|
|
|
|
|
|
try:
|
|
|
|
parse_ctx.fragment.set_key_value(parse_ctx.key, stmts)
|
|
|
|
except Exception as e:
|
2020-06-10 01:53:51 -04:00
|
|
|
raise ParseFatalException(pstr, loc, "unable to add key '%s'; %s" % (parse_ctx.key, str(e)))
|
2019-03-19 02:35:47 -04:00
|
|
|
return None
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
key = Word(alphanums + '_') + Suppress(':')
|
2019-03-19 02:35:47 -04:00
|
|
|
key_stmt = Forward()
|
|
|
|
|
|
|
|
condition_block = indentedBlock(key_stmt, indent_stack)
|
|
|
|
key_stmts = OneOrMore(condition_block)
|
|
|
|
key_body = Suppress(key) + key_stmts
|
|
|
|
key_body.setParseAction(key_body_parsed)
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
condition = originalTextFor(SDKConfig.get_expression_grammar()).setResultsName('condition')
|
|
|
|
if_condition = Group(Suppress('if') + condition + Suppress(':') + condition_block)
|
|
|
|
elif_condition = Group(Suppress('elif') + condition + Suppress(':') + condition_block)
|
|
|
|
else_condition = Group(Suppress('else') + Suppress(':') + condition_block)
|
|
|
|
conditional = (if_condition + Optional(OneOrMore(elif_condition)) + Optional(else_condition)).setResultsName('conditional')
|
2019-03-19 02:35:47 -04:00
|
|
|
|
|
|
|
def key_parse_action(pstr, loc, toks):
|
|
|
|
key = toks[0]
|
|
|
|
|
|
|
|
if key in parse_ctx.keys:
|
|
|
|
raise ParseFatalException(pstr, loc, "duplicate key '%s' value definition" % parse_ctx.key)
|
|
|
|
|
|
|
|
parse_ctx.key = key
|
|
|
|
parse_ctx.keys.append(key)
|
|
|
|
|
|
|
|
try:
|
|
|
|
parse_ctx.key_grammar = parse_ctx.fragment.get_key_grammars()[key]
|
|
|
|
key_grammar = parse_ctx.key_grammar.grammar
|
|
|
|
except KeyError:
|
|
|
|
raise ParseFatalException(pstr, loc, "key '%s' is not supported by fragment" % key)
|
|
|
|
except Exception as e:
|
2020-06-10 01:53:51 -04:00
|
|
|
raise ParseFatalException(pstr, loc, "unable to parse key '%s'; %s" % (key, str(e)))
|
2019-03-19 02:35:47 -04:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
key_stmt << (conditional | Group(key_grammar).setResultsName('value'))
|
2019-03-19 02:35:47 -04:00
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def name_parse_action(pstr, loc, toks):
|
|
|
|
parse_ctx.fragment.name = toks[0]
|
|
|
|
|
|
|
|
key.setParseAction(key_parse_action)
|
|
|
|
|
|
|
|
ftype = Word(alphas).setParseAction(fragment_type_parse_action)
|
2021-01-25 21:49:01 -05:00
|
|
|
fid = Suppress(':') + Word(alphanums + '_.').setResultsName('name')
|
2019-03-19 02:35:47 -04:00
|
|
|
fid.setParseAction(name_parse_action)
|
2021-01-25 21:49:01 -05:00
|
|
|
header = Suppress('[') + ftype + fid + Suppress(']')
|
2019-03-19 02:35:47 -04:00
|
|
|
|
|
|
|
def fragment_parse_action(pstr, loc, toks):
|
|
|
|
key_grammars = parse_ctx.fragment.get_key_grammars()
|
|
|
|
required_keys = set([k for (k,v) in key_grammars.items() if v.required])
|
|
|
|
present_keys = required_keys.intersection(set(parse_ctx.keys))
|
|
|
|
if present_keys != required_keys:
|
2021-01-25 21:49:01 -05:00
|
|
|
raise ParseFatalException(pstr, loc, 'required keys %s for fragment not found' %
|
2019-03-19 02:35:47 -04:00
|
|
|
list(required_keys - present_keys))
|
|
|
|
return parse_ctx.fragment
|
|
|
|
|
|
|
|
fragment_stmt = Forward()
|
|
|
|
fragment_block = indentedBlock(fragment_stmt, indent_stack)
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
fragment_if_condition = Group(Suppress('if') + condition + Suppress(':') + fragment_block)
|
|
|
|
fragment_elif_condition = Group(Suppress('elif') + condition + Suppress(':') + fragment_block)
|
|
|
|
fragment_else_condition = Group(Suppress('else') + Suppress(':') + fragment_block)
|
2019-03-19 02:35:47 -04:00
|
|
|
fragment_conditional = (fragment_if_condition + Optional(OneOrMore(fragment_elif_condition)) +
|
2021-01-25 21:49:01 -05:00
|
|
|
Optional(fragment_else_condition)).setResultsName('conditional')
|
2019-03-19 02:35:47 -04:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
fragment = (header + OneOrMore(indentedBlock(key_body, indent_stack, False))).setResultsName('value')
|
2019-03-19 02:35:47 -04:00
|
|
|
fragment.setParseAction(fragment_parse_action)
|
2021-01-25 21:49:01 -05:00
|
|
|
fragment.ignore('#' + restOfLine)
|
2019-03-19 02:35:47 -04:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
deprecated_mapping = DeprecatedMapping.get_fragment_grammar(sdkconfig, fragment_file.name).setResultsName('value')
|
2019-04-02 04:40:43 -04:00
|
|
|
|
|
|
|
fragment_stmt << (Group(deprecated_mapping) | Group(fragment) | Group(fragment_conditional))
|
2019-03-19 02:35:47 -04:00
|
|
|
|
|
|
|
def fragment_stmt_parsed(pstr, loc, toks):
|
|
|
|
stmts = list()
|
|
|
|
expand_conditionals(toks, stmts)
|
|
|
|
return stmts
|
|
|
|
|
|
|
|
parser = ZeroOrMore(fragment_stmt)
|
|
|
|
parser.setParseAction(fragment_stmt_parsed)
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2019-03-19 02:35:47 -04:00
|
|
|
self.fragments = parser.parseFile(fragment_file, parseAll=True)
|
2018-04-17 22:57:45 -04:00
|
|
|
|
|
|
|
for fragment in self.fragments:
|
|
|
|
fragment.path = path
|
|
|
|
|
2018-12-04 07:46:48 -05:00
|
|
|
|
2019-03-19 02:35:47 -04:00
|
|
|
class Fragment():
|
|
|
|
__metaclass__ = abc.ABCMeta
|
2018-12-04 07:46:48 -05:00
|
|
|
"""
|
|
|
|
Encapsulates a fragment as defined in the generator syntax. Sets values common to all fragment and performs processing
|
|
|
|
such as checking the validity of the fragment name and getting the entry values.
|
|
|
|
"""
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
IDENTIFIER = Word(alphas + '_', alphanums + '_')
|
2019-06-06 04:55:46 -04:00
|
|
|
ENTITY = Word(alphanums + '.-_$+')
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2019-03-19 02:35:47 -04:00
|
|
|
@abc.abstractmethod
|
|
|
|
def set_key_value(self, key, parse_results):
|
|
|
|
pass
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2019-03-19 02:35:47 -04:00
|
|
|
@abc.abstractmethod
|
|
|
|
def get_key_grammars(self):
|
|
|
|
pass
|
2018-12-04 07:46:48 -05:00
|
|
|
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2019-03-19 02:35:47 -04:00
|
|
|
class Sections(Fragment):
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2020-10-22 06:51:03 -04:00
|
|
|
# Unless quoted, symbol names start with a letter, underscore, or point
|
|
|
|
# and may include any letters, underscores, digits, points, and hyphens.
|
2021-01-25 21:49:01 -05:00
|
|
|
GNU_LD_SYMBOLS = Word(alphas + '_.', alphanums + '._-')
|
2020-10-22 06:51:03 -04:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
entries_grammar = Combine(GNU_LD_SYMBOLS + Optional('+'))
|
2020-10-22 06:51:03 -04:00
|
|
|
|
2019-03-19 02:35:47 -04:00
|
|
|
grammars = {
|
2021-01-25 21:49:01 -05:00
|
|
|
'entries': KeyGrammar(entries_grammar.setResultsName('section'), 1, None, True)
|
2019-03-19 02:35:47 -04:00
|
|
|
}
|
2018-04-17 22:57:45 -04:00
|
|
|
|
|
|
|
"""
|
|
|
|
Utility function that returns a list of sections given a sections fragment entry,
|
|
|
|
with the '+' notation and symbol concatenation handled automatically.
|
|
|
|
"""
|
|
|
|
@staticmethod
|
|
|
|
def get_section_data_from_entry(sections_entry, symbol=None):
|
|
|
|
if not symbol:
|
|
|
|
sections = list()
|
2021-01-25 21:49:01 -05:00
|
|
|
sections.append(sections_entry.replace('+', ''))
|
|
|
|
sections.append(sections_entry.replace('+', '.*'))
|
2018-04-17 22:57:45 -04:00
|
|
|
return sections
|
|
|
|
else:
|
2021-01-25 21:49:01 -05:00
|
|
|
if sections_entry.endswith('+'):
|
|
|
|
section = sections_entry.replace('+', '.*')
|
|
|
|
expansion = section.replace('.*', '.' + symbol)
|
2018-04-17 22:57:45 -04:00
|
|
|
return (section, expansion)
|
|
|
|
else:
|
|
|
|
return (sections_entry, None)
|
|
|
|
|
2019-03-19 02:35:47 -04:00
|
|
|
def set_key_value(self, key, parse_results):
|
2021-01-25 21:49:01 -05:00
|
|
|
if key == 'entries':
|
2019-03-19 02:35:47 -04:00
|
|
|
self.entries = set()
|
|
|
|
for result in parse_results:
|
2021-01-25 21:49:01 -05:00
|
|
|
self.entries.add(result['section'])
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2019-03-19 02:35:47 -04:00
|
|
|
def get_key_grammars(self):
|
|
|
|
return self.__class__.grammars
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2018-12-04 07:46:48 -05:00
|
|
|
|
2018-04-17 22:57:45 -04:00
|
|
|
class Scheme(Fragment):
|
2018-12-04 07:46:48 -05:00
|
|
|
"""
|
|
|
|
Encapsulates a scheme fragment, which defines what target input sections are placed under.
|
|
|
|
"""
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2019-03-19 02:35:47 -04:00
|
|
|
grammars = {
|
2021-01-25 21:49:01 -05:00
|
|
|
'entries': KeyGrammar(Fragment.IDENTIFIER.setResultsName('sections') + Suppress('->') +
|
|
|
|
Fragment.IDENTIFIER.setResultsName('target'), 1, None, True)
|
2019-03-19 02:35:47 -04:00
|
|
|
}
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2019-03-19 02:35:47 -04:00
|
|
|
def set_key_value(self, key, parse_results):
|
2021-01-25 21:49:01 -05:00
|
|
|
if key == 'entries':
|
2019-03-19 02:35:47 -04:00
|
|
|
self.entries = set()
|
|
|
|
for result in parse_results:
|
2021-01-25 21:49:01 -05:00
|
|
|
self.entries.add((result['sections'], result['target']))
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2019-03-19 02:35:47 -04:00
|
|
|
def get_key_grammars(self):
|
|
|
|
return self.__class__.grammars
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2018-12-04 07:46:48 -05:00
|
|
|
|
2018-04-17 22:57:45 -04:00
|
|
|
class Mapping(Fragment):
|
2018-12-04 07:46:48 -05:00
|
|
|
"""
|
|
|
|
Encapsulates a mapping fragment, which defines what targets the input sections of mappable entties are placed under.
|
|
|
|
"""
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
MAPPING_ALL_OBJECTS = '*'
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2019-04-14 07:34:27 -04:00
|
|
|
def __init__(self):
|
|
|
|
Fragment.__init__(self)
|
|
|
|
self.entries = set()
|
2019-04-05 02:32:21 -04:00
|
|
|
self.deprecated = False
|
2019-04-14 07:34:27 -04:00
|
|
|
|
2019-03-19 02:35:47 -04:00
|
|
|
def set_key_value(self, key, parse_results):
|
2021-01-25 21:49:01 -05:00
|
|
|
if key == 'archive':
|
|
|
|
self.archive = parse_results[0]['archive']
|
|
|
|
elif key == 'entries':
|
2019-03-19 02:35:47 -04:00
|
|
|
for result in parse_results:
|
|
|
|
obj = None
|
2018-04-17 22:57:45 -04:00
|
|
|
symbol = None
|
2019-03-19 02:35:47 -04:00
|
|
|
scheme = None
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2019-03-19 02:35:47 -04:00
|
|
|
try:
|
2021-01-25 21:49:01 -05:00
|
|
|
obj = result['object']
|
2019-03-19 02:35:47 -04:00
|
|
|
except KeyError:
|
|
|
|
pass
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2019-03-19 02:35:47 -04:00
|
|
|
try:
|
2021-01-25 21:49:01 -05:00
|
|
|
symbol = result['symbol']
|
2019-03-19 02:35:47 -04:00
|
|
|
except KeyError:
|
|
|
|
pass
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2019-03-19 02:35:47 -04:00
|
|
|
try:
|
2021-01-25 21:49:01 -05:00
|
|
|
scheme = result['scheme']
|
2019-03-19 02:35:47 -04:00
|
|
|
except KeyError:
|
|
|
|
pass
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2019-03-19 02:35:47 -04:00
|
|
|
self.entries.add((obj, symbol, scheme))
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2019-03-19 02:35:47 -04:00
|
|
|
def get_key_grammars(self):
|
2018-04-17 22:57:45 -04:00
|
|
|
# There are three possible patterns for mapping entries:
|
|
|
|
# obj:symbol (scheme)
|
|
|
|
# obj (scheme)
|
|
|
|
# * (scheme)
|
2021-01-25 21:49:01 -05:00
|
|
|
obj = Fragment.ENTITY.setResultsName('object')
|
|
|
|
symbol = Suppress(':') + Fragment.IDENTIFIER.setResultsName('symbol')
|
|
|
|
scheme = Suppress('(') + Fragment.IDENTIFIER.setResultsName('scheme') + Suppress(')')
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2019-03-19 02:35:47 -04:00
|
|
|
pattern1 = obj + symbol + scheme
|
|
|
|
pattern2 = obj + scheme
|
2021-01-25 21:49:01 -05:00
|
|
|
pattern3 = Literal(Mapping.MAPPING_ALL_OBJECTS).setResultsName('object') + scheme
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2019-03-19 02:35:47 -04:00
|
|
|
entry = pattern1 | pattern2 | pattern3
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2019-03-19 02:35:47 -04:00
|
|
|
grammars = {
|
2021-01-25 21:49:01 -05:00
|
|
|
'archive': KeyGrammar(Fragment.ENTITY.setResultsName('archive'), 1, 1, True),
|
|
|
|
'entries': KeyGrammar(entry, 0, None, True)
|
2019-03-19 02:35:47 -04:00
|
|
|
}
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2019-03-19 02:35:47 -04:00
|
|
|
return grammars
|
2018-04-17 22:57:45 -04:00
|
|
|
|
|
|
|
|
2019-04-02 04:40:43 -04:00
|
|
|
class DeprecatedMapping():
|
|
|
|
"""
|
|
|
|
Encapsulates a mapping fragment, which defines what targets the input sections of mappable entties are placed under.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Name of the default condition entry
|
2021-01-25 21:49:01 -05:00
|
|
|
DEFAULT_CONDITION = 'default'
|
|
|
|
MAPPING_ALL_OBJECTS = '*'
|
2019-04-02 04:40:43 -04:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_fragment_grammar(sdkconfig, fragment_file):
|
|
|
|
|
|
|
|
# Match header [mapping]
|
2021-01-25 21:49:01 -05:00
|
|
|
header = Suppress('[') + Suppress('mapping') + Suppress(']')
|
2019-04-02 04:40:43 -04:00
|
|
|
|
|
|
|
# There are three possible patterns for mapping entries:
|
|
|
|
# obj:symbol (scheme)
|
|
|
|
# obj (scheme)
|
|
|
|
# * (scheme)
|
2021-01-25 21:49:01 -05:00
|
|
|
obj = Fragment.ENTITY.setResultsName('object')
|
|
|
|
symbol = Suppress(':') + Fragment.IDENTIFIER.setResultsName('symbol')
|
|
|
|
scheme = Suppress('(') + Fragment.IDENTIFIER.setResultsName('scheme') + Suppress(')')
|
2019-04-02 04:40:43 -04:00
|
|
|
|
|
|
|
pattern1 = Group(obj + symbol + scheme)
|
|
|
|
pattern2 = Group(obj + scheme)
|
2021-01-25 21:49:01 -05:00
|
|
|
pattern3 = Group(Literal(Mapping.MAPPING_ALL_OBJECTS).setResultsName('object') + scheme)
|
2019-04-02 04:40:43 -04:00
|
|
|
|
|
|
|
mapping_entry = pattern1 | pattern2 | pattern3
|
|
|
|
|
|
|
|
# To simplify parsing, classify groups of condition-mapping entry into two types: normal and default
|
|
|
|
# A normal grouping is one with a non-default condition. The default grouping is one which contains the
|
|
|
|
# default condition
|
2021-01-25 21:49:01 -05:00
|
|
|
mapping_entries = Group(ZeroOrMore(mapping_entry)).setResultsName('mappings')
|
2019-04-02 04:40:43 -04:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
normal_condition = Suppress(':') + originalTextFor(SDKConfig.get_expression_grammar())
|
|
|
|
default_condition = Optional(Suppress(':') + Literal(DeprecatedMapping.DEFAULT_CONDITION))
|
2019-04-02 04:40:43 -04:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
normal_group = Group(normal_condition.setResultsName('condition') + mapping_entries)
|
|
|
|
default_group = Group(default_condition + mapping_entries).setResultsName('default_group')
|
2019-04-02 04:40:43 -04:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
normal_groups = Group(ZeroOrMore(normal_group)).setResultsName('normal_groups')
|
2019-04-02 04:40:43 -04:00
|
|
|
|
|
|
|
# Any mapping fragment definition can have zero or more normal group and only one default group as a last entry.
|
2021-01-25 21:49:01 -05:00
|
|
|
archive = Suppress('archive') + Suppress(':') + Fragment.ENTITY.setResultsName('archive')
|
|
|
|
entries = Suppress('entries') + Suppress(':') + (normal_groups + default_group).setResultsName('entries')
|
2019-04-02 04:40:43 -04:00
|
|
|
|
|
|
|
mapping = Group(header + archive + entries)
|
2021-01-25 21:49:01 -05:00
|
|
|
mapping.ignore('#' + restOfLine)
|
2019-04-02 04:40:43 -04:00
|
|
|
|
|
|
|
def parsed_deprecated_mapping(pstr, loc, toks):
|
|
|
|
fragment = Mapping()
|
|
|
|
fragment.archive = toks[0].archive
|
2021-01-25 21:49:01 -05:00
|
|
|
fragment.name = re.sub(r'[^0-9a-zA-Z]+', '_', fragment.archive)
|
2019-04-05 02:32:21 -04:00
|
|
|
fragment.deprecated = True
|
2019-04-02 04:40:43 -04:00
|
|
|
|
|
|
|
fragment.entries = set()
|
|
|
|
condition_true = False
|
|
|
|
for entries in toks[0].entries[0]:
|
|
|
|
condition = next(iter(entries.condition.asList())).strip()
|
|
|
|
condition_val = sdkconfig.evaluate_expression(condition)
|
|
|
|
|
|
|
|
if condition_val:
|
|
|
|
for entry in entries[1]:
|
|
|
|
fragment.entries.add((entry.object, None if entry.symbol == '' else entry.symbol, entry.scheme))
|
|
|
|
condition_true = True
|
|
|
|
break
|
|
|
|
|
|
|
|
if not fragment.entries and not condition_true:
|
|
|
|
try:
|
|
|
|
entries = toks[0].entries[1][1]
|
|
|
|
except IndexError:
|
|
|
|
entries = toks[0].entries[1][0]
|
|
|
|
for entry in entries:
|
|
|
|
fragment.entries.add((entry.object, None if entry.symbol == '' else entry.symbol, entry.scheme))
|
|
|
|
|
|
|
|
if not fragment.entries:
|
2021-01-25 21:49:01 -05:00
|
|
|
fragment.entries.add(('*', None, 'default'))
|
2019-04-02 04:40:43 -04:00
|
|
|
|
|
|
|
dep_warning = str(ParseFatalException(pstr, loc,
|
2021-01-25 21:49:01 -05:00
|
|
|
'Warning: Deprecated old-style mapping fragment parsed in file %s.' % fragment_file))
|
2019-04-02 04:40:43 -04:00
|
|
|
|
|
|
|
print(dep_warning)
|
|
|
|
return fragment
|
|
|
|
|
|
|
|
mapping.setParseAction(parsed_deprecated_mapping)
|
|
|
|
return mapping
|
|
|
|
|
|
|
|
|
2019-03-19 02:35:47 -04:00
|
|
|
FRAGMENT_TYPES = {
|
2021-01-25 21:49:01 -05:00
|
|
|
'sections': Sections,
|
|
|
|
'scheme': Scheme,
|
|
|
|
'mapping': Mapping
|
2019-03-19 02:35:47 -04:00
|
|
|
}
|