php-kilo/src/FileType.php

210 lines
8.8 KiB
PHP

<?php declare(strict_types=1);
namespace Aviat\Kilo;
use Aviat\Kilo\Enum\SyntaxFamily;
class FileType {
/**
* Create the FileType object from the filename
*/
public static function from(?string $filename): self
{
$syntax = self::getSyntaxFromFilename((string)$filename);
return new self($syntax->filetype, $syntax);
}
/**
* Create the Syntax object from the filename
*/
private static function getSyntaxFromFilename(string $filename): Syntax
{
$ext = strstr(basename($filename), '.');
$ext = ($ext !== FALSE) ? $ext : '';
return match ($ext) {
'.sh', '.bash' => Syntax::new(
'Shell',
[
'case', 'do', 'done', 'elif', 'else', 'esac', 'fi', 'for', 'function', 'if', 'in',
'select', 'then', 'time', 'until', 'while', 'declare',
],
['set'],
operators: ['[[', ']]'],
slcs: '#',
mcs: '',
mce: '',
highlightNumbers: false,
),
'.php', 'kilo' => Syntax::new(
'PHP',
),
'.c', '.h', '.cpp', '.cxx', '.cc', '.hpp' => Syntax::new(
'C',
[
'auto', 'break', 'case', 'const', 'continue', 'default', 'do', 'typedef', 'switch', 'return',
'static', 'while', 'break', 'struct', 'extern', 'union', 'class', 'else', 'enum', 'for', 'case',
'if', 'inline', 'register', 'restrict', 'return', 'sizeof', 'switch', 'typedef', 'union', 'volatile'
],
[
'#include', 'unsigned', '#define', '#ifndef', 'double', 'signed', '#endif',
'#ifdef', 'float', '#error', '#undef', '#elif', 'long', 'char', 'int', 'void', '#if',
'uint32_t', 'wchar_t', 'int32_t', 'int64_t', 'uint64_t', 'int16_t', 'uint16_t',
'uint8_t', 'int8_t', 'time_t', 'size_t',
],
[
'<=>', '<<=', '>>=',
'++', '--', '==', '!=', '>=', '<=', '&&', '||', '<<', '>>',
'+=', '-=', '*=', '/=', '%=', '&=', '|=', '^=', '->', '::',
],
hasCharType: true,
highlightCharacters: true,
),
'.css', '.less', '.sass', '.scss' => Syntax::new(
'CSS',
slcs: '',
hasCommonOperators: false,
),
'.go' => Syntax::new(
'Go',
[
'break', 'case', 'chan', 'const', 'continue', 'default', 'defer', 'else',
'fallthrough', 'for', 'func', 'go', 'goto', 'if', 'import', 'interface',
'map', 'package', 'range', 'return', 'select', 'struct', 'switch', 'type', 'var',
],
[
'uint8', 'uint16', 'uint32', 'uint64', 'int8', 'int16', 'int32', 'int64', 'float32', 'float64',
'uint', 'int', 'uintptr', 'complex64', 'complex128', 'byte', 'rune', '[]',
],
[
'&^=', '...', '>>=', '<<=', '--', '%=', '>>', ':=', '++', '/=', '<<', '>=',
'<-', '^=', '*=', '<=', '||', '|=', '-=', '!=', '==', '&&', '&=', '+=',
],
hasCharType: true,
highlightCharacters: true,
),
'.js', '.jsx', '.ts', '.tsx', '.jsm', '.mjs', '.es' => Syntax::new(
'JavaScript',
[
'instanceof', 'continue', 'debugger', 'function', 'default', 'extends',
'finally', 'delete', 'export', 'import', 'return', 'switch', 'typeof',
'break', 'catch', 'class', 'const', 'super', 'throw', 'while', 'yield',
'case', 'else', 'this', 'void', 'with', 'from', 'for', 'new', 'try',
'var', 'do', 'if', 'in', 'as',
],
[
'=>', 'Number', 'String', 'Object', 'Math', 'JSON', 'Boolean',
],
),
'.rs' => Syntax::new(
'Rust',
[
'continue', 'return', 'static', 'struct', 'unsafe', 'break', 'const', 'crate',
'extern', 'match', 'super', 'trait', 'where', 'else', 'enum', 'false', 'impl',
'loop', 'move', 'self', 'type', 'while', 'for', 'let', 'mod', 'pub', 'ref', 'true',
'use', 'mut', 'as', 'fn', 'if', 'in',
],
[
'DoubleEndedIterator', 'ExactSizeIterator', 'IntoIterator', 'PartialOrd', 'PartialEq',
'Iterator', 'ToString', 'Default', 'ToOwned', 'Extend', 'FnOnce', 'Option', 'String',
'AsMut', 'AsRef', 'Clone', 'Debug', 'FnMut', 'Sized', 'Unpin', 'array', 'isize',
'usize', '&str', 'Copy', 'Drop', 'From', 'Into', 'None', 'Self', 'Send', 'Some',
'Sync', 'bool', 'char', 'i128', 'u128', 'Box', 'Err', 'Ord', 'Vec', 'dyn', 'f32',
'f64', 'i16', 'i32', 'i64', 'str', 'u16', 'u32', 'u64', 'Eq', 'Fn', 'Ok', 'i8', 'u8',
'&mut self', '&mut', '&self', 'self',
],
[
'...', '=>', '..', '>>=', '<<=', '--', '%=', '>>', ':=', '++', '/=', '<<', '>=',
'<-', '^=', '*=', '<=', '||', '|=', '-=', '!=', '==', '&&', '&=', '+=', '..=',
'.',
],
hasCharType: true,
highlightCharacters: true,
),
'.html', '.htm', '.shtml' => Syntax::new(
'Html',
[
'!doctype', '!DOCTYPE', 'html', 'base', 'head', 'link', 'meta', 'style', 'title', 'body', 'address', 'article',
'aside', 'footer', 'header', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'main', 'nav', 'section',
'blockquote', 'dd', 'div', 'dl', 'dt', 'figcaption', 'figure', 'hr', 'li', 'ol', 'p', 'pre',
'ul', 'a', 'abbr', 'b', 'bdi', 'bdo', 'br', 'cite', 'code', 'data', 'dfn', 'em', 'i', 'kbd',
'mark', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'small', 'span', 'strong', 'sub', 'sup', 'time',
'u', 'var', 'wbr', 'area', 'audio', 'img', 'map', 'track', 'video', 'embed', 'iframe', 'object',
'param', 'picture', 'portal', 'source', 'svg', 'math', 'canvas', 'noscript', 'script', 'del',
'ins', 'caption', 'col', 'colgroup', 'table', 'td', 'tbody', 'tfoot', 'th', 'thead', 'tr',
'button', 'datalist', 'fieldset', 'form', 'input', 'label', 'legend', 'meter', 'optgroup',
'option', 'output', 'progress', 'select', 'textarea', 'details', 'dialog', 'menu', 'summary',
'slot', 'template', 'frameset', 'frame', 'noframes', 'marquee', 'font', 'center',
],
[
'id', 'class', 'for', 'style', 'charset'
],
operators: ['</', '<', '=', '>'],
slcs: '',
mcs: '<!--',
mce: '-->',
highlightNumbers: false,
hasCommonOperators: false,
syntaxFamily: SyntaxFamily::XML,
),
'.xml' => Syntax::new(
'XML',
[
'!doctype', '!element', '<?xml', '?>',
],
operators: ['<', '=', '>'],
slcs: '',
mcs: '<!--',
mce: '-->',
highlightNumbers: false,
hasCommonOperators: false,
syntaxFamily: SyntaxFamily::XML,
),
'.zig' => Syntax::new(
'Zig',
[
'auto', 'break', 'case', 'const', 'continue', 'default', 'do', 'typedef', 'switch', 'return',
'static', 'while', 'break', 'struct', 'extern', 'union', 'class', 'else', 'enum', 'for', 'case',
'if', 'inline', 'register', 'restrict', 'return', 'sizeof', 'switch', 'typedef', 'union', 'volatile',
'pub', 'fn', 'orelse', 'catch', 'and', 'or', 'comptime', 'test', 'var', 'opaque',
'usingnamespace', 'errdefer', 'callconv', 'unreachable', 'defer', 'inline',
'@addrSpaceCast', '@addWithOverflow', '@alignCast', '@alignOf', '@as', '@atomicLoad', '@atomicRmw',
'@atomicStore', '@bitCast', '@bitOffsetOf', '@bitSizeOf', '@breakpoint', '@mulAdd', '@byteSwap',
'@bitReverse', '@offsetOf', '@call', '@cDefine', '@cImport', '@cInclude', '@clz', '@cmpxchgStrong',
'@cmpxchgWeak', '@compileError', '@compileLog', '@constCast', '@ctz', '@cUndef', '@cVaArg', '@cVaCopy',
'@cVaEnd', '@cVaStart', '@divExact', '@divFloor', '@divTrunc', '@embedFile', '@enumFromInt', '@errorFromInt',
'@errorName', '@errorReturnTrace', '@errorCast', '@export', '@extern', '@fence', '@field', '@fieldParentPtr',
'@floatCast', '@floatFromInt', '@frameAddress', '@hasDecl', '@hasField', '@import', '@inComptime',
'@intCast', '@intFromBool', '@intFromEnum', '@intFromError', '@intFromFloat', '@intFromPtr', '@max',
'@memcpy', '@memset', '@min', "@wasmMemorySize", '@wasmMemoryGrow', '@mod', '@mulWithOverflow', '@panic',
'@popCount', '@prefetch', '@ptrCast', '@ptrFromInt', '@rem', '@returnAddress', '@select', '@setAlignStack',
'@setCold', '@setEvalBranchQuota', '@setFloatMode', '@setRuntimeSafety', '@shlExact', '@shlWithOverflow',
'@shrExact', '@shuffle', '@sizeOf', '@splat', '@reduce', '@src', '@sqrt', '@sin', '@cos', '@tan', '@exp',
'@exp2', '@log', '@log2', '@log10', '@abs', '@floor', '@ceil', '@trunc', '@round', '@subWithOverflow',
'@tagName', '@This', '@trap', '@truncate', '@Type', '@typeInfo', '@typeName', '@TypeOf', '@unionint',
'@Vector', '@volatileCast', '@workGroupId', '@workGroupSize', '@workItemId',
],
[
'i8', 'u8', 'i16', 'u16', 'i32', 'u32', 'i64', 'u64', 'i128', 'u128', 'isize', 'usize',
'c_char', 'c_short', 'c_ushort', 'c_int', 'c_uint', 'c_long', 'c_ulong', 'c_longlong',
'c_ulonglong', 'c_longdouble',
'f16', 'f32', 'f64', 'f80', 'f128', 'bool', 'anyopaque', 'void', 'noreturn', 'type',
'anyerror', 'comptime_init', 'comptime_float',
],
[
'<=>', '<<=', '>>=', '...',
'++', '--', '==', '!=', '>=', '<=', '&&', '||', '<<', '>>',
'+=', '-=', '*=', '/=', '%=', '&=', '|=', '^=', '->', '::',
'+%', '+%=', '+|', '+|=', '-%', '-%=', '*%', '*%=', '*|', '*|=',
'.?', '++', '**', '.*',
],
mcs: '',
mce: '',
),
default => Syntax::default(),
};
}
private function __construct(public string $name, public Syntax $syntax) {}
}