2021-03-16 18:37:53 -04:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Aviat\Kilo;
|
|
|
|
|
2021-08-13 11:17:35 -04:00
|
|
|
use Aviat\Kilo\Enum\SyntaxFamily;
|
|
|
|
|
2021-03-16 18:37:53 -04:00
|
|
|
class FileType {
|
2021-04-14 19:00:37 -04:00
|
|
|
/**
|
|
|
|
* Create the FileType object from the filename
|
|
|
|
*
|
|
|
|
* @param string|null $filename
|
|
|
|
* @return self
|
|
|
|
*/
|
2021-03-16 18:37:53 -04:00
|
|
|
public static function from(?string $filename): self
|
|
|
|
{
|
|
|
|
$syntax = self::getSyntaxFromFilename((string)$filename);
|
|
|
|
|
|
|
|
return new self($syntax->filetype, $syntax);
|
|
|
|
}
|
|
|
|
|
2021-04-14 19:00:37 -04:00
|
|
|
/**
|
|
|
|
* Create the Syntax object from the filename
|
|
|
|
*
|
|
|
|
* @param string $filename
|
|
|
|
* @return Syntax
|
|
|
|
*/
|
2021-03-16 18:37:53 -04:00
|
|
|
private static function getSyntaxFromFilename(string $filename): Syntax
|
|
|
|
{
|
2021-03-17 15:38:52 -04:00
|
|
|
$ext = strstr(basename($filename), '.');
|
|
|
|
$ext = ($ext !== FALSE) ? $ext : '';
|
2021-03-16 18:37:53 -04:00
|
|
|
return match ($ext) {
|
2021-04-14 12:25:41 -04:00
|
|
|
'.sh', '.bash' => Syntax::new(
|
|
|
|
'Shell',
|
2021-04-14 17:19:01 -04:00
|
|
|
[
|
|
|
|
'case', 'do', 'done', 'elif', 'else', 'esac', 'fi', 'for', 'function', 'if', 'in',
|
|
|
|
'select', 'then', 'time', 'until', 'while', 'declare',
|
|
|
|
],
|
|
|
|
['set'],
|
2021-04-14 18:38:33 -04:00
|
|
|
operators: ['[[', ']]'],
|
2021-04-14 12:25:41 -04:00
|
|
|
slcs: '#',
|
|
|
|
mcs: '',
|
|
|
|
mce: '',
|
2021-04-14 17:19:01 -04:00
|
|
|
highlightNumbers: false,
|
2021-04-14 12:25:41 -04:00
|
|
|
),
|
2021-03-16 18:37:53 -04:00
|
|
|
'.php', 'kilo' => Syntax::new(
|
|
|
|
'PHP',
|
|
|
|
),
|
|
|
|
'.c', '.h', '.cpp', '.cxx', '.cc', '.hpp' => Syntax::new(
|
|
|
|
'C',
|
|
|
|
[
|
2021-04-09 21:19:46 -04:00
|
|
|
'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'
|
2021-03-16 18:37:53 -04:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'#include', 'unsigned', '#define', '#ifndef', 'double', 'signed', '#endif',
|
2021-04-09 21:19:46 -04:00
|
|
|
'#ifdef', 'float', '#error', '#undef', '#elif', 'long', 'char', 'int', 'void', '#if',
|
|
|
|
'uint32_t', 'wchar_t', 'int32_t', 'int64_t', 'uint64_t', 'int16_t', 'uint16_t',
|
2021-04-15 12:47:02 -04:00
|
|
|
'uint8_t', 'int8_t', 'time_t', 'size_t',
|
2021-04-09 21:19:46 -04:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'<=>', '<<=', '>>=',
|
|
|
|
'++', '--', '==', '!=', '>=', '<=', '&&', '||', '<<', '>>',
|
|
|
|
'+=', '-=', '*=', '/=', '%=', '&=', '|=', '^=', '->', '::',
|
2021-03-16 18:37:53 -04:00
|
|
|
],
|
2021-04-09 19:40:28 -04:00
|
|
|
hasCharType: true,
|
|
|
|
highlightCharacters: true,
|
2021-03-16 18:37:53 -04:00
|
|
|
),
|
|
|
|
'.css', '.less', '.sass', '.scss' => Syntax::new(
|
|
|
|
'CSS',
|
|
|
|
slcs: '',
|
2021-04-14 17:19:01 -04:00
|
|
|
hasCommonOperators: false,
|
2021-03-16 18:37:53 -04:00
|
|
|
),
|
2021-04-09 19:40:28 -04:00
|
|
|
'.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', '[]',
|
|
|
|
],
|
2021-04-09 21:19:46 -04:00
|
|
|
[
|
|
|
|
'&^=', '...', '>>=', '<<=', '--', '%=', '>>', ':=', '++', '/=', '<<', '>=',
|
|
|
|
'<-', '^=', '*=', '<=', '||', '|=', '-=', '!=', '==', '&&', '&=', '+=',
|
|
|
|
],
|
2021-04-09 19:40:28 -04:00
|
|
|
hasCharType: true,
|
|
|
|
highlightCharacters: true,
|
|
|
|
),
|
2021-03-16 18:37:53 -04:00
|
|
|
'.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',
|
2021-04-14 18:38:33 -04:00
|
|
|
'&mut self', '&mut', '&self', 'self',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'...', '=>', '..', '>>=', '<<=', '--', '%=', '>>', ':=', '++', '/=', '<<', '>=',
|
|
|
|
'<-', '^=', '*=', '<=', '||', '|=', '-=', '!=', '==', '&&', '&=', '+=', '..=',
|
|
|
|
'.',
|
2021-03-16 18:37:53 -04:00
|
|
|
],
|
2021-04-09 19:40:28 -04:00
|
|
|
hasCharType: true,
|
|
|
|
highlightCharacters: true,
|
2021-03-16 18:37:53 -04:00
|
|
|
),
|
2021-08-13 11:17:35 -04:00
|
|
|
'.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,
|
|
|
|
),
|
2021-03-16 18:37:53 -04:00
|
|
|
default => Syntax::default(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private function __construct(public string $name, public Syntax $syntax) {}
|
|
|
|
}
|