master
ブランチ master
LittleBeast / src / Std / Lib / Syntax / Swift.php
3.1 KB、55行 生 Blame 履歴
1<?php
2namespace Std\Lib\Syntax;
3
4class Swift extends Language {
5 protected bool $nestedComments = true;
6 protected array $strings = ['"'];
7 protected string $digitSep = '_';
8
9 protected array $rules = [
10 // 生文字列 #"…"#(複数行の#"""…"""#も)
11 ['re' => '(?<rawhash>#+)"""(?s:.*?)"""\k<rawhash>|(?<rawhash2>#+)"(?:[^"\n]|"(?!\k<rawhash2>))*"\k<rawhash2>', 'class' => 'string'],
12 // 複数行の文字列 """…"""
13 ['re' => '"""(?s:.*?)(?:"""|\z)', 'class' => 'string'],
14 // コンパイラ指令(#if, #available, #selector等)
15 ['re' => '#[A-Za-z_]\w*', 'class' => 'preprocessor'],
16 // 属性(@main, @MainActor, @escaping等)
17 ['re' => '@[A-Za-z_]\w*', 'class' => 'attribute'],
18 // 予約語を名前にする `default`
19 ['re' => '`[^`\n]+`', 'class' => null],
20 ];
21
22 protected array $tokens = [
23 'keyword' => [
24 // 宣言
25 'associatedtype', 'class', 'deinit', 'enum', 'extension', 'fileprivate', 'func', 'import', 'init', 'inout', 'internal', 'let',
26 'open', 'operator', 'private', 'precedencegroup', 'protocol', 'public', 'rethrows', 'static', 'struct', 'subscript', 'typealias',
27 'var', 'actor', 'macro', 'package', 'nonisolated', 'isolated', 'consuming', 'borrowing', 'sending',
28 // 文
29 'break', 'case', 'catch', 'continue', 'default', 'defer', 'do', 'else', 'fallthrough', 'for', 'guard', 'if', 'in', 'repeat',
30 'return', 'throw', 'switch', 'where', 'while',
31 // 式・型
32 'as', 'await', 'is', 'self', 'Self', 'super', 'throws', 'try', 'async', 'some', 'any', 'Type', 'Protocol',
33 // 文脈依存
34 'convenience', 'dynamic', 'didSet', 'final', 'get', 'indirect', 'lazy', 'mutating', 'nonmutating', 'optional', 'override',
35 'postfix', 'prefix', 'infix', 'required', 'set', 'unowned', 'weak', 'willSet',
36 ],
37 'constant' => [
38 'true', 'false', 'nil',
39 ],
40 'type' => [
41 'Int', 'Int8', 'Int16', 'Int32', 'Int64', 'Int128', 'UInt', 'UInt8', 'UInt16', 'UInt32', 'UInt64', 'UInt128',
42 'Float', 'Float16', 'Float32', 'Float64', 'Float80', 'Double', 'CGFloat', 'Bool', 'String', 'Substring', 'Character',
43 'Array', 'ContiguousArray', 'Dictionary', 'Set', 'Optional', 'Result', 'Range', 'ClosedRange', 'Void', 'Never', 'Any', 'AnyObject',
44 'Error', 'Equatable', 'Hashable', 'Comparable', 'Codable', 'Encodable', 'Decodable', 'Identifiable', 'Sendable', 'Sequence',
45 'Collection', 'IteratorProtocol', 'CustomStringConvertible', 'Numeric', 'BinaryInteger', 'FloatingPoint', 'SIMD2', 'SIMD3', 'SIMD4',
46 'UnsafePointer', 'UnsafeMutablePointer', 'UnsafeRawPointer', 'UnsafeMutableRawPointer', 'UnsafeBufferPointer', 'OpaquePointer',
47 'Data', 'Date', 'URL', 'UUID', 'NSObject', 'Task', 'MainActor', 'View', 'some',
48 ],
49 'builtin' => [
50 'print', 'debugPrint', 'dump', 'assert', 'assertionFailure', 'precondition', 'preconditionFailure', 'fatalError',
51 'min', 'max', 'abs', 'stride', 'zip', 'sequence', 'repeatElement', 'swap', 'type', 'withUnsafePointer',
52 'withUnsafeMutablePointer', 'withUnsafeBytes', 'withExtendedLifetime', 'MemoryLayout', 'readLine', 'numericCast',
53 ],
54 ];
55}