| 1 | <?php |
| 2 | namespace Std\Lib\Syntax; |
| 3 | |
| 4 | class Cpp extends C { |
| 5 | protected array $headerDirectives = ['include', 'include_next', 'embed', 'import']; |
| 6 | |
| 7 | protected array $rules = [ |
| 8 | // 生文字列 R"区切り(…)区切り" |
| 9 | ['re' => '(?:u8|[uUL])?R"(?<rawdelim>[^()\\\\\s]{0,16})\((?s:.*?)\)\k<rawdelim>"', 'class' => 'string'], |
| 10 | // 属性 [[nodiscard]] |
| 11 | ['re' => '\[\[[^\]\n]*\]\]', 'class' => 'attribute'], |
| 12 | ]; |
| 13 | |
| 14 | /** @var array C++のトークン(Cの関数・型は prepare() で追加する) */ |
| 15 | protected array $cppTokens = [ |
| 16 | 'keyword' => [ |
| 17 | // C++26 |
| 18 | 'contract_assert', 'pre', 'post', 'trivially_relocatable_if_eligible', 'replaceable_if_eligible', |
| 19 | // C++20 |
| 20 | 'char8_t', 'concept', 'consteval', 'constinit', 'co_await', 'co_return', 'co_yield', 'import', 'module', 'requires', |
| 21 | // C++11 |
| 22 | 'alignas', 'alignof', 'char16_t', 'char32_t', 'constexpr', 'decltype', 'final', 'noexcept', 'nullptr', 'override', 'static_assert', 'thread_local', |
| 23 | // C++98 |
| 24 | 'and', 'and_eq', 'asm', 'auto', 'bitand', 'bitor', 'bool', 'break', 'case', 'catch', 'char', 'class', 'compl', 'const', 'const_cast', 'continue', |
| 25 | 'default', 'delete', 'do', 'double', 'dynamic_cast', 'else', 'enum', 'explicit', 'export', 'extern', 'false', 'float', 'for', 'friend', 'goto', 'if', 'inline', |
| 26 | 'int', 'long', 'mutable', 'namespace', 'new', 'not', 'not_eq', 'operator', 'or', 'or_eq', 'private', 'protected', 'public', 'register', |
| 27 | 'reinterpret_cast', 'return', 'short', 'signed', 'sizeof', 'static', 'static_cast', 'struct', 'switch', 'template', 'this', 'throw', 'true', 'try', |
| 28 | 'typedef', 'typeid', 'typename', 'union', 'unsigned', 'using', 'virtual', 'void', 'volatile', 'wchar_t', 'while', 'xor', 'xor_eq', |
| 29 | // TMTS |
| 30 | 'atomic_cancel', 'atomic_commit', 'atomic_noexcept', 'synchronized', 'transaction_safe', 'transaction_safe_dynamic', |
| 31 | ], |
| 32 | 'type' => [ |
| 33 | 'std', 'chrono', 'filesystem', 'ranges', 'views', 'this_thread', 'numbers', 'execution', 'pmr', |
| 34 | 'string', 'wstring', 'u8string', 'u16string', 'u32string', 'string_view', 'wstring_view', |
| 35 | 'vector', 'array', 'deque', 'list', 'forward_list', 'map', 'multimap', 'set', 'multiset', |
| 36 | 'unordered_map', 'unordered_multimap', 'unordered_set', 'unordered_multiset', 'flat_map', 'flat_set', |
| 37 | 'stack', 'queue', 'priority_queue', 'span', 'mdspan', 'bitset', 'valarray', |
| 38 | 'pair', 'tuple', 'optional', 'variant', 'any', 'expected', 'unexpected', 'monostate', 'nullopt_t', 'nullptr_t', 'byte', |
| 39 | 'unique_ptr', 'shared_ptr', 'weak_ptr', 'function', 'move_only_function', 'reference_wrapper', 'initializer_list', |
| 40 | 'thread', 'jthread', 'mutex', 'recursive_mutex', 'shared_mutex', 'lock_guard', 'unique_lock', 'scoped_lock', 'condition_variable', |
| 41 | 'atomic', 'atomic_flag', 'future', 'promise', 'stop_token', 'stop_source', |
| 42 | 'istream', 'ostream', 'iostream', 'ifstream', 'ofstream', 'fstream', 'stringstream', 'istringstream', 'ostringstream', |
| 43 | 'size_t', 'ptrdiff_t', 'intptr_t', 'uintptr_t', 'int8_t', 'int16_t', 'int32_t', 'int64_t', 'uint8_t', 'uint16_t', 'uint32_t', 'uint64_t', |
| 44 | 'float16_t', 'float32_t', 'float64_t', 'float128_t', 'bfloat16_t', 'max_align_t', |
| 45 | 'exception', 'runtime_error', 'logic_error', 'invalid_argument', 'out_of_range', 'bad_alloc', 'error_code', |
| 46 | 'iterator', 'const_iterator', 'reverse_iterator', 'allocator', 'hash', 'less', 'greater', 'equal_to', |
| 47 | 'duration', 'time_point', 'steady_clock', 'system_clock', 'high_resolution_clock', 'path', 'regex', 'complex', 'ratio', |
| 48 | ], |
| 49 | 'builtin' => [ |
| 50 | 'cout', 'cin', 'cerr', 'clog', 'wcout', 'wcin', 'endl', 'flush', 'ws', 'getline', 'print', 'println', 'format', |
| 51 | 'move', 'forward', 'swap', 'exchange', 'make_unique', 'make_shared', 'make_pair', 'make_tuple', 'make_optional', 'tie', 'get', |
| 52 | 'static_pointer_cast', 'dynamic_pointer_cast', 'reinterpret_pointer_cast', 'addressof', 'launder', 'bit_cast', 'to_underlying', |
| 53 | 'sort', 'stable_sort', 'find', 'find_if', 'count', 'count_if', 'copy', 'copy_if', 'fill', 'transform', 'accumulate', 'reduce', |
| 54 | 'for_each', 'remove', 'remove_if', 'reverse', 'unique', 'min', 'max', 'clamp', 'min_element', 'max_element', 'lower_bound', 'upper_bound', |
| 55 | 'all_of', 'any_of', 'none_of', 'iota', 'to_string', 'stoi', 'stol', 'stof', 'stod', 'invoke', 'apply', 'visit', 'holds_alternative', |
| 56 | 'declval', 'is_same_v', 'is_integral_v', 'is_floating_point_v', 'enable_if_t', 'conditional_t', 'decay_t', 'remove_reference_t', |
| 57 | 'numeric_limits', 'begin', 'end', 'cbegin', 'cend', 'rbegin', 'rend', 'size', 'ssize', 'empty', 'data', 'unreachable', |
| 58 | ], |
| 59 | ]; |
| 60 | |
| 61 | /** |
| 62 | * C++のトークンに、Cのライブラリ関数と型を加える(ヘッダーでは絞らない) |
| 63 | */ |
| 64 | public function prepare(string $code): void { |
| 65 | $tokens = $this->cppTokens; |
| 66 | foreach (['builtin', 'type'] as $k) { |
| 67 | foreach ($this->tokens[$k] ?? [] as $list) { |
| 68 | foreach ($list as $w) $tokens[$k][] = $w; |
| 69 | } |
| 70 | } |
| 71 | $tokens['preprocessor'] = $this->tokens['preprocessor'] ?? []; |
| 72 | $this->words = $this->buildWords(['constant' => $this->constants] + $tokens); |
| 73 | } |
| 74 | } |