#!/usr/bin/env python3 # # Copyright 2014 The Chromium Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file.
import collections from datetime import date import re import optparse import os from string import Template import sys import textwrap import zipfile
from util import build_utils from util import java_cpp_utils import action_helpers # build_utils adds //build to sys.path. import zip_helpers
# List of C++ types that are compatible with the Java code generated by this # script. # # This script can parse .idl files however, at present it ignores special # rules such as [cpp_enum_prefix_override="ax_attr"].
ENUM_FIXED_TYPE_ALLOWLIST = [ 'char', 'unsigned char', 'short', 'unsigned short', 'int', 'int8_t', 'int16_t', 'int32_t', 'uint8_t', 'uint16_t'
]
class EnumDefinition:
def __init__(self,
original_enum_name=None,
class_name_override=None,
enum_package=None,
entries=None,
comments=None,
fixed_type=None,
is_flag=False): """Represents a C++ enum that must be converted to java.
Args:
original_enum_name: The name of the enum itself, without its package. If every entry starts with this value, this prefix is removed.
class_name_override: the name for the enum in java. IfNone, the original enum name is used.
enum_package: The java package in which this enum must be defined
entries: A list of pairs. Each pair contains an enum entry, followed by
either Noneor the value of this entry. The definition could be, for
example, an integer, an expression `2 << 5`, or another enun entry.
comments: A list of pairs. Each pair contains an entry and a comment
associated to this entry.
fixed_type: The type encoding this enum. Should belong to
`ENUM_FIXED_TYPE_ALLOWLIST`.
is_flag: Whether this value is used as a boolean flag whose entries can
be xored together. """
self.original_enum_name = original_enum_name
self.class_name_override = class_name_override
self.enum_package = enum_package
self.entries = collections.OrderedDict(entries or [])
self.comments = collections.OrderedDict(comments or [])
self.prefix_to_strip = None
self.fixed_type = fixed_type
self.is_flag = is_flag
def AppendEntry(self, key, value): if key in self.entries: raise Exception('Multiple definitions of key %s found.' % key)
self.entries[key] = value
def AppendEntryComment(self, key, value): if key in self.comments: raise Exception('Multiple definitions of key %s found.' % key)
self.comments[key] = value
@property def class_name(self): return self.class_name_override or self.original_enum_name
def _Validate(self): assert self.class_name assert self.enum_package assert self.entries if self.fixed_type and self.fixed_type notin ENUM_FIXED_TYPE_ALLOWLIST: raise Exception('Fixed type %s for enum %s not in allowlist.' %
(self.fixed_type, self.class_name))
def _AssignEntryIndices(self): # Enums, if given no value, are given the value of the previous enum + 1. ifnot all(self.entries.values()):
prev_enum_value = -1 for key, value in self.entries.items(): ifnot value:
self.entries[key] = prev_enum_value + 1 elif value in self.entries:
self.entries[key] = self.entries[value] else: try:
self.entries[key] = int(value) except ValueError as e: raise Exception('Could not interpret integer from enum value "%s" ' 'for key %s.' % (value, key)) from e
prev_enum_value = self.entries[key]
# "kMaxValue" is a special enum entry representing the last value of an # histogram enum. It is not expected to have prefix even when other values # have a prefix.
standard_keys = [key for key in self.entries.keys() if key != "kMaxValue"]
for prefix in prefixes: if all(w.startswith(prefix) for w in standard_keys):
prefix_to_strip = prefix break else:
prefix_to_strip = ''
def StripEntries(entries):
ret = collections.OrderedDict() for k, v in entries.items():
stripped_key = k.replace(prefix_to_strip, '', 1) if isinstance(v, str):
stripped_value = v.replace(prefix_to_strip, '') else:
stripped_value = v
ret[stripped_key] = stripped_value
def _TransformKeys(d, func): """Normalize keys in |d| and update references to old keys in |d| values."""
keys_map = {k: func(k) for k in d}
ret = collections.OrderedDict() for k, v in d.items(): # Need to transform values as well when the entry value was explicitly set # (since it could contain references to other enum entry values). if isinstance(v, str): # First check if a full replacement is available. This avoids issues when # one key is a substring of another. if v in d:
v = keys_map[v] else: for old_key, new_key in keys_map.items():
v = v.replace(old_key, new_key)
ret[keys_map[k]] = v return ret
class HeaderParser:
single_line_comment_re = re.compile(r'\s*//\s*([^\n]*)')
multi_line_comment_start_re = re.compile(r'\s*/\*')
enum_line_re = re.compile(r'^\s*(\w+)(\s*\=\s*([^,\n]+))?,?')
enum_end_re = re.compile(r'^\s*}\s*;\.*$') # Note: For now we only support a very specific `#if` statement to prevent the # possibility of miscalculating whether lines should be ignored when building # for Android.
if_buildflag_re = re.compile(
r'^#if BUILDFLAG\((\w+)\)(?: \|\| BUILDFLAG\((\w+)\))*$')
if_buildflag_end_re = re.compile(r'^#endif.*$')
generator_error_re = re.compile(r'^\s*//\s+GENERATED_JAVA_(\w+)\s*:\s*$')
generator_directive_re = re.compile(
r'^\s*//\s+GENERATED_JAVA_(\w+)\s*:\s*([\.\w]+)$')
multi_line_generator_directive_start_re = re.compile(
r'^\s*//\s+GENERATED_JAVA_(\w+)\s*:\s*\(([\.\w]*)$')
multi_line_directive_continuation_re = re.compile(r'^\s*//\s+([\.\w]+)$')
multi_line_directive_end_re = re.compile(r'^\s*//\s+([\.\w]*)\)$')
def _ParseEnumLine(self, line): if HeaderParser.multi_line_comment_start_re.match(line): raise Exception('Multi-line comments in enums are not supported in ' +
self._path)
enum_comment = HeaderParser.single_line_comment_re.match(line) if enum_comment:
comment = enum_comment.groups()[0] if comment:
self._current_comments.append(comment) elif HeaderParser.enum_end_re.match(line):
self._FinalizeCurrentEnumDefinition() else:
self._AddToCurrentEnumEntry(line) if','in line:
self._ParseCurrentEnumEntry()
def _ParseSingleLineEnum(self, line): for entry in line.split(','):
self._AddToCurrentEnumEntry(entry)
self._ParseCurrentEnumEntry()
if generator_directive_error: raise Exception('Malformed directive declaration in ' + self._path + '. Use () for multi-line directives. E.g.\n' + '// GENERATED_JAVA_ENUM_PACKAGE: (\n' + '// foo.package)') if generator_directive:
directive_name = generator_directive.groups()[0]
directive_value = generator_directive.groups()[1]
self._generator_directives.Update(directive_name, directive_value) elif multi_line_generator_directive_start:
directive_name = multi_line_generator_directive_start.groups()[0]
directive_value = multi_line_generator_directive_start.groups()[1]
self._multi_line_generator_directive = (directive_name, [directive_value]) elif enum_start or single_line_enum: if self._generator_directives.empty: return
self._current_definition = EnumDefinition(
original_enum_name=enum_start.groups()[1],
fixed_type=enum_start.groups()[3])
self._in_enum = True if single_line_enum:
self._ParseSingleLineEnum(single_line_enum.group('enum_entries'))
def DoGenerate(source_paths): for source_path in source_paths:
enum_definitions = DoParseHeaderFile(source_path) ifnot enum_definitions: raise Exception('No enums found in %s\n' 'Did you forget prefixing enums with ' '"// GENERATED_JAVA_ENUM_PACKAGE: foo"?' %
source_path) for enum_definition in enum_definitions:
output_path = java_cpp_utils.GetJavaFilePath(enum_definition.enum_package,
enum_definition.class_name)
output = GenerateOutput(source_path, enum_definition) yield output_path, output
def DoParseHeaderFile(path): with open(path) as f: return HeaderParser(f.readlines(), path).ParseDefinitions()
def GenerateOutput(source_path, enum_definition):
template = Template("""
// Copyright ${YEAR} The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This file is autogenerated by
// ${SCRIPT_NAME}
// From
// ${SOURCE_PATH}
parser.add_option('--srcjar',
help='When specified, a .srcjar at the given path is ' 'created instead of individual .java files.')
options, args = parser.parse_args(argv)
ifnot args:
parser.error('Need to specify at least one input file')
input_paths = args
with action_helpers.atomic_output(options.srcjar) as f: with zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED) as srcjar: for output_path, data in DoGenerate(input_paths):
zip_helpers.add_to_zip_hermetic(srcjar, output_path, data=data)
if __name__ == '__main__':
DoMain(sys.argv[1:])
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.18 Sekunden
(vorverarbeitet am 2026-08-28)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.