php-kilo/src/FileType.php

108 lines
3.8 KiB
PHP

<?php declare(strict_types=1);
namespace Aviat\Kilo;
class FileType {
public static function from(?string $filename): self
{
$syntax = self::getSyntaxFromFilename((string)$filename);
return new self($syntax->filetype, $syntax);
}
private static function getSyntaxFromFilename(string $filename): Syntax
{
$ext = strstr(basename($filename), '.');
$ext = ($ext !== FALSE) ? $ext : '';
return match ($ext) {
'.sh', '.bash' => Syntax::new(
'Shell',
slcs: '#',
mcs: '',
mce: '',
),
'.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',
],
[
'<=>', '<<=', '>>=',
'++', '--', '==', '!=', '>=', '<=', '&&', '||', '<<', '>>',
'+=', '-=', '*=', '/=', '%=', '&=', '|=', '^=', '->', '::',
],
hasCharType: true,
highlightCharacters: true,
),
'.css', '.less', '.sass', '.scss' => Syntax::new(
'CSS',
slcs: '',
),
'.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',
],
hasCharType: true,
highlightCharacters: true,
),
default => Syntax::default(),
};
}
private function __construct(public string $name, public Syntax $syntax) {}
}