131e6177e1
Some checks failed
timw4mail/php-kilo/pipeline/head There was a failure building this commit
174 lines
6.2 KiB
PHP
174 lines
6.2 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace Aviat\Kilo;
|
|
|
|
use Aviat\Kilo\Enum\SyntaxFamily;
|
|
|
|
class FileType {
|
|
/**
|
|
* Create the FileType object from the filename
|
|
*
|
|
* @param string|null $filename
|
|
* @return self
|
|
*/
|
|
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
|
|
*
|
|
* @param string $filename
|
|
* @return Syntax
|
|
*/
|
|
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,
|
|
),
|
|
default => Syntax::default(),
|
|
};
|
|
}
|
|
|
|
private function __construct(public string $name, public Syntax $syntax) {}
|
|
} |