Ejemplo n.º 1
0
 public function testInAll()
 {
     $rule = Validator::everywhere();
     $this->assertTrue($rule->validate(Context::fromArray(['first'])));
     $this->assertTrue($rule->validate(Context::fromArray(['second'])));
     $this->assertTrue($rule->validate(Context::fromArray([])));
     $this->assertTrue($rule->validate(Context::fromArray(['other'])));
 }
Ejemplo n.º 2
0
 /**
  * Tokenization rules setup
  */
 public function setupRules()
 {
     parent::setupRules();
     $this->rules->validator = new Validator(['!format.block.code', '!format.monospace', '!operator.escape', '!operator', '!tag']);
     $this->rules->addMany(['format.header' => [new Rule(new RegexMatcher('/^\\s{0,3}(#+.+?)\\r?$/m')), new Rule(new RegexMatcher('/^([^\\R]+?)^(?:-+|=+)\\R?$/m'))], 'format.italics' => new Rule(new RegexMatcher('/(?:^|[^*_])(?P<italics>(?P<i>[*_])(?>[^*_\\R]|(?:(?P<b>[*_]{2})(?>[^*_\\R]|(?&italics))*?\\g{b}))+\\g{i})/'), ['italics' => Token::NAME]), 'format.bold' => new Rule(new RegexMatcher('/(?P<bold>(?P<b>\\*\\*|__)(?>[^*_\\R]|(?:(?P<i>[*_]{2})(?>[^*_\\R]|(?&bold))*?\\g{i}))+\\g{b})/', ['bold' => Token::NAME])), 'format.strike' => new Rule(new RegexMatcher('/(~~.+?~~)/')), 'format.monospace' => [new Rule(new RegexMatcher('/(?:[^`]|^)(`.*?`)/')), new Rule(new RegexMatcher('/(``.*?``)/')), new Rule(new RegexMatcher('/^((?:(?: {4,}|\\t).*?(?>\\R|$)+?)+)/m'))], 'operator.list.ordered' => new Rule(new RegexMatcher('/^\\s*(\\d+[.)])/m')), 'operator.list.unordered' => new Rule(new RegexMatcher('/^\\s*([-+*])/m'), ['context' => ['none'], 'priority' => 1]), 'string.quote' => new Rule(new RegexMatcher('/((?:^>.*?\\R)+)/m')), 'format.block.code' => [new Rule(new DelegateRegexMatcher('/^```(.*?)\\R(.*?)\\R^```/ms', function ($match, TokenFactoryInterface $factory) {
         $lang = KeyLighter::get()->getLanguage($match[1][0]);
         (yield $factory->create(Token::NAME, ['pos' => $match[0][1], 'length' => strlen($match[0][0])]));
         (yield $factory->create("language.{$lang->getIdentifier()}", ['pos' => $match[2][1], 'length' => strlen($match[2][0]), 'inject' => $lang, 'class' => LanguageToken::class]));
     }), ['context' => Validator::everywhere(), 'postProcess' => true, 'priority' => 1000])], 'operator.escape' => new Rule(new RegexMatcher('/(\\\\.)/')), 'operator.horizontal' => new Rule(new RegexMatcher('/^\\s{,3}(([-*_])( ?\\2)+)\\R/m'), ['priority' => 2]), 'symbol.link' => new Rule(new RegexMatcher('/[^!](\\[(?:[^\\[\\]]|!\\[.*?\\]\\(.*?\\))*\\])/i')), 'symbol.url' => new Rule(new RegexMatcher('#(<(https?://|[^/])[-A-Za-z0-9+&@\\#/%?=~_|!:,.;]+[-A-Za-z0-9+&@\\#/%=~_|]>|https?://[-A-Za-z0-9+&@\\#/%?=~_|!:,.;]+[-A-Za-z0-9+&@\\#/%=~_|])#i'), ['priority' => 0]), 'symbol.image' => new Rule(new RegexMatcher('/(!)(\\[.*?\\])\\(.*?\\)/i', [1 => 'operator.image', 2 => '$.title']))]);
 }
Ejemplo n.º 3
0
 /**
  * Tokenization rules
  */
 public function setupRules()
 {
     parent::setupRules();
     $this->rules->remove('meta.declaration');
     $this->rules->remove('meta.declaration.media');
     $this->rules->add('meta.selector', new Rule(new RegexMatcher('/(?=(?:\\n+|^)(\\h*)([^\\h].*)\\n+\\1\\h+)/', [2 => Token::NAME]), ['context' => Validator::everywhere(), 'priority' => 3, 'factory' => new TokenFactory(MetaToken::class)]));
     $this->rules->add('meta.declaration', new Rule(new RegexMatcher('/\\n((?:\\h+.*?(?>\\n|$)+)+)/'), ['context' => Validator::everywhere(), 'priority' => 2, 'factory' => new TokenFactory(MetaToken::class)]));
     $this->rules->add('meta.declaration.media', new Rule(new RegexMatcher('/@media(.*?)\\n/'), ['context' => Validator::everywhere(), 'factory' => new TokenFactory(MetaToken::class)]));
     $this->rules->add('symbol.selector.tag', new Rule(new RegexMatcher('/\\b([a-z-][\\w-]*)/'), ['context' => ['meta.selector', '!symbol', '!meta.declaration.media']]));
     $this->rules->rule('symbol.selector.class')->setContext(['meta.selector']);
     $this->rules->rule('keyword.at-rule')->setContext(['meta.selector']);
     $this->rules->rule('symbol.selector.class.pseudo')->setContext(['meta.selector']);
     $this->rules->rule('symbol.selector.id')->setContext(['meta.selector']);
 }
Ejemplo n.º 4
0
 /**
  * Tokenization rules setup
  */
 public function setupRules()
 {
     $this->rules->addMany(['keyword' => new Rule(new WordMatcher(['auto', 'break', 'case', 'const', 'continue', 'default', 'do', 'else', 'enum', 'extern', 'for', 'goto', 'if', 'register', 'return', 'sizeof', 'static', 'struct', 'switch', 'typedef', 'union', 'volatile', 'while'])), 'meta.newline' => new CloseRule(new RegexMatcher('/()\\R/m'), ['factory' => new TokenFactory(TerminatorToken::class), 'context' => ['!operator.escape', '*string', '*call.preprocessor'], 'closes' => ['string.double', 'preprocessor']]), 'preprocessor' => new OpenRule(new RegexMatcher('/^(#)/m'), ['context' => Validator::everywhere()]), 'call' => ['preprocessor' => new Rule(new RegexMatcher('/^#\\s*(\\w+)\\b/m'), ['context' => ['preprocessor']]), new Rule(new RegexMatcher('/([a-z_]\\w*)\\s*\\(/i'), ['priority' => -1])], 'operator.escape' => new Rule(new RegexMatcher('/(\\\\(?:.|[0-7]{3}|x\\x{2}))/s'), ['context' => Validator::everywhere()]), 'keyword.format' => new Rule(new RegexMatcher('/(%[diuoxXfFeEgGaAcspn%][-+#0]?(?:[0-9]+|\\*)?(?:\\.(?:[0-9]+|\\*))?)/'), ['context' => ['string']]), 'string' => array_merge([new Rule(new RegexMatcher('/(<.*?>)/'), ['context' => ['preprocessor']])], CommonFeatures::strings(['single' => '\'', 'double' => '"'], ['context' => ['!operator.escape', '!comment', '!string']])), 'symbol.type' => [new Rule(new RegexMatcher('/(\\w+)(?:\\s+|\\s*\\*\\s*)\\w+\\s*[=();,]/')), new Rule(new WordMatcher(['int', 'float', 'double', 'char', 'void', 'long', 'short', 'signed', 'unsigned']))], 'comment' => [new Rule(new CommentMatcher(['//'], [['/*', '*/']]), ['priority' => 2])], 'variable.property' => new Rule(new RegexMatcher('/(?=(?:\\w|\\)|\\])\\s*(?:->|\\.)\\s*([a-z_]\\w*))/'), ['priority' => 0]), 'number' => new Rule(new RegexMatcher('/\\b(-?(?:0x[\\da-f]+|\\d*\\.?\\d+(?:e[+-]?\\d+)?)[ful]*)\\b/i')), 'operator' => ['punctuation' => new Rule(new RegexMatcher('/([;,])/'), ['priority' => 0]), new Rule(new RegexMatcher('/([*&])/'), ['priority' => 0]), new Rule(new RegexMatcher('/([!+\\-\\/*&|^<>=]{1,2}=?)/'), ['priority' => 0])]]);
 }
Ejemplo n.º 5
0
 /**
  * Tokenization rules
  */
 public function setupRules()
 {
     $identifier = '-?[_a-zA-Z]+[_a-zA-Z0-9-]*';
     $at = ['charset', 'import', 'namespace', 'media', 'supports', 'document', 'page', 'font-face', 'keyframes', 'viewport', 'counter-style', 'font-feature-values', 'swash', 'ornaments', 'annotation', 'stylistic', 'styleset', 'character-variant'];
     $this->rules->addMany(['string' => CommonFeatures::strings(['single' => '\'', 'double' => '"'], ['context' => $this->everywhere()]), 'meta.declaration' => [new OpenRule(new SubStringMatcher('{'), ['context' => ['!meta.declaration.media', '!comment'], 'factory' => new TokenFactory(MetaToken::class)]), new CloseRule(new SubStringMatcher('}'))], 'meta.declaration.media' => [new Rule(new RegexMatcher('/@media(.*?\\{)/'), ['context' => Validator::everywhere(), 'factory' => new TokenFactory(MetaToken::class)])], 'meta.declaration.rule' => [new OpenRule(new RegexMatcher('/@media.*(\\()/'), ['context' => ['meta.declaration.media'], 'factory' => new TokenFactory(MetaToken::class)]), new CloseRule(new SubStringMatcher(')'))], 'keyword.at-rule' => new Rule(new RegexMatcher('/(@(?:-[a-z]+-)?(?:' . implode('|', $at) . '))/'), ['priority' => 2]), 'symbol.selector.id' => new Rule(new RegexMatcher("/(#{$identifier})/i")), 'symbol.selector.tag' => new Rule(new RegexMatcher('/(?>[\\s}]|^)(?=(\\w+)[^;]*\\{)/ms')), 'symbol.selector.class' => new Rule(new RegexMatcher("/(\\.{$identifier})/i")), 'symbol.selector.class.pseudo' => new Rule(new RegexMatcher("/(:{1,2}{$identifier})/")), 'number' => new Rule(new RegexMatcher("/([-+]?[0-9]*\\.?[0-9]+([\\w%]+)?)/"), ['context' => ['meta.declaration', '!constant.color', '!comment', '!symbol', '!comment', '!string'], 'priority' => 0]), 'symbol.property' => new Rule(new RegexMatcher("/({$identifier}:)/"), ['context' => ['meta.declaration', '!symbol', '!comment'], 'priority' => 2]), 'call' => new Rule(new RegexMatcher("/({$identifier})\\s*\\((?:(?P<string>[a-z].*?)|.*?)\\)/", [1 => Token::NAME, 'string' => 'string.argument']), ['context' => ['meta.declaration', '!comment', '!string', '!keyword']]), 'constant.color' => [new Rule(new RegexMatcher("/(#[0-9a-f]{3,6})/i"), ['priority' => 2, 'context' => ['meta.declaration', '!comment']]), new Rule(new WordMatcher(['white', 'silver', 'gray', 'black', 'red', 'maroon', 'yellow', 'olive', 'lime', 'green', 'aqua', 'teal', 'blue', 'navy', 'fuchsia', 'purple']), ['context' => ['meta.declaration', '!comment', '!symbol', '!variable']])], 'operator' => new Rule(new WordMatcher(['>', '+', '*', '!important'], ['separated' => false]), ['context' => $this->everywhere()]), 'operator.punctuation' => new Rule(new SubStringMatcher(';'), ['context' => $this->everywhere()]), 'comment' => new Rule(new CommentMatcher([], [['/*', '*/']]), ['context' => $this->everywhere()])]);
 }
Ejemplo n.º 6
0
 /**
  * Tokenization rules setup
  */
 public function setupRules()
 {
     $this->rules->addMany(['call' => new Rule(new RegexMatcher('/(?>(?<![^\\\\]\\\\)(?<=\\n|\\(|\\||;|`|^|do|if|then|else|\\$\\(|^\\$\\s)\\s*(\\w+))(?!\\s*=)/im'), ['priority' => 1, 'context' => ['*none', '*expression']]), 'comment' => new Rule(new CommentMatcher(['#'])), 'string' => CommonFeatures::strings(['single' => '\'', 'double' => '"']), 'keyword' => new Rule(new WordMatcher(['if', 'then', 'else', 'elif', 'fi', 'case', 'esac', 'for', 'select', 'while', 'until', 'do', 'done', 'in', 'function', 'time', 'coproc']), ['priority' => 3]), 'variable' => ['assign' => new Rule(new RegexMatcher('/(\\w+)\\s*=/')), new Rule(new RegexMatcher('/(\\$\\w+)/i'), ['context' => ['*none', '*string.double']])], 'number' => new Rule(new RegexMatcher('/(-?(?:0[0-7]+|0[xX][0-9a-fA-F]+|0b[01]+|\\d+))/')), 'delimiter' => new Rule(new RegexMatcher('/^(\\$)/m')), 'symbol.parameter' => new Rule(new RegexMatcher('/\\s(-{1,2}\\w+=?)\\b/i'), ['priority' => 0, 'context' => ['!string', '!comment', '!call']]), 'expression' => [new Rule(new RegexMatcher('/(?=(\\$\\(((?>[^$()]+|(?1))+)\\)))/x'), ['context' => Validator::everywhere(), 'factory' => new TokenFactory(LanguageToken::class), 'inject' => $this])], 'operator.escape' => new Rule(new RegexMatcher('/(\\\\(?:x[0-9a-fA-F]{1,2}|u\\{[0-9a-fA-F]{1,6}\\}|[0-7]{1,3}|.))/i'), ['context' => ['string']])]);
 }
Ejemplo n.º 7
0
 public function getEnds($embedded = false)
 {
     return ['variable' => [new OpenRule(new SubStringMatcher('{{'), ['factory' => new TokenFactory(LanguageToken::class), 'priority' => 1000, 'inject' => $this, 'context' => Validator::everywhere(), 'language' => null]), new CloseRule(new SubStringMatcher('}}'), ['factory' => new TokenFactory(LanguageToken::class), 'priority' => 1000, 'language' => $this])], 'template-tag' => [new OpenRule(new SubStringMatcher('{%'), ['factory' => new TokenFactory(LanguageToken::class), 'inject' => $this, 'priority' => 1000, 'context' => Validator::everywhere(), 'language' => null]), new CloseRule(new SubStringMatcher('%}'), ['factory' => new TokenFactory(LanguageToken::class), 'language' => $this, 'priority' => 1000])], 'comment' => [new OpenRule(new SubStringMatcher('{#'), ['factory' => new TokenFactory(LanguageToken::class), 'inject' => $this, 'priority' => 1000, 'language' => null]), new CloseRule(new SubStringMatcher('#}'), ['factory' => new TokenFactory(LanguageToken::class), 'language' => $this, 'priority' => 1000])]];
 }
Ejemplo n.º 8
0
 /**
  * Language range Rule(s)
  *
  * @param $embedded
  *
  * @return Rule|\Kadet\Highlighter\Parser\Rule[]
  */
 public function getEnds($embedded = false)
 {
     return new Rule(new WholeMatcher(), ['priority' => 10000, 'factory' => new TokenFactory(LanguageToken::class), 'inject' => $this, 'language' => null, 'context' => Validator::everywhere()]);
 }
Ejemplo n.º 9
0
 /**
  * Tokenization rules setup
  */
 public function setupRules()
 {
     $standard = new Validator(['!string', '!comment']);
     $this->rules->addMany(['keyword' => new Rule(new WordMatcher(['and', 'del', 'from', 'not', 'while', 'as', 'elif', 'global', 'or', 'with', 'assert', 'else', 'if', 'pass', 'yield', 'break', 'except', 'import', 'print', 'class', 'exec', 'in', 'raise', 'continue', 'finally', 'is', 'return', 'def', 'for', 'lambda', 'try'])), 'operator' => new Rule(new RegexMatcher('/([-+%=]=?|!=|\\*\\*?=?|\\/\\/?=?|<[<=>]?|>[=>]?|[&|^~])|\\b(or|and|not)\\b/'), ['priority' => -1]), 'expression' => new Rule(new RegexMatcher('/\\{(\\S+)\\}/'), ['context' => ['string']]), 'variable' => [new Rule(new RegexMatcher('/[^\\w.]([a-z_]\\w*)\\.\\w/i')), 'property' => new Rule(new RegexMatcher('/(?=(?:\\w|\\)|\\])\\s*\\.([a-z_]\\w*))/i'), ['priority' => -2, 'context' => ['*none', '*expression']])], 'symbol' => [new Rule(new RegexMatcher('/import\\s+([a-z_][\\w.]*)(?:\\s*,\\s*([a-z_][\\w.]*))*/i', [1 => Token::NAME, 2 => Token::NAME])), 'library' => new Rule(new RegexMatcher('/from\\s+([a-z_][\\w.]*)\\s+import/i', [1 => Token::NAME]))], 'operator.escape' => new Rule(new RegexMatcher('/(\\\\(?:.|[0-7]{3}|x\\x{2}))/'), ['context' => Validator::everywhere()]), 'comment' => new Rule(new CommentMatcher(['#'])), 'constant.special' => [new Rule(new WordMatcher(['True', 'False', 'NotImplemented', 'Ellipsis'], ['case-sensitivity' => true])), new Rule(new RegexMatcher('/\\b(__\\w+__)\\b/'))], 'call' => new Rule(new RegexMatcher('/([a-z_]\\w*)\\s*\\(/i'), ['priority' => 2]), 'meta.newline' => new CloseRule(new RegexMatcher('/()\\R/'), ['factory' => new TokenFactory(TerminatorToken::class), 'context' => ['!operator.escape', 'string.single-line'], 'closes' => ['string.single-line.double', 'string.single-line.single']]), 'number' => new Rule(new RegexMatcher('/(-?\\b(?:0[bo])?(?:(?:\\d|0x[\\da-f])[\\da-f]*\\.?\\d*|\\.\\d+)(?:e[+-]?\\d+)?j?)\\b/')), 'string' => ['single-line' => ['double' => new Rule(new RegexMatcher('/(?:^|[^"])(")(?=[^"]|$)/'), ['factory' => new TokenFactory(ContextualToken::class), 'context' => $standard]), 'single' => new Rule(new RegexMatcher('/(?:^|[^\'])(\')(?=[^\']|$)/'), ['factory' => new TokenFactory(ContextualToken::class), 'context' => $standard])], 'multi-line' => ['double' => new Rule(new SubStringMatcher('"""'), ['factory' => new TokenFactory(ContextualToken::class), 'context' => $standard, 'priority' => 2]), 'single' => new Rule(new SubStringMatcher('\'\'\''), ['factory' => new TokenFactory(ContextualToken::class), 'context' => $standard, 'priority' => 2])]], 'symbol.function' => new Rule(new RegexMatcher('/def\\s+([a-z_]\\w+)\\s*\\(/i')), 'symbol.class' => new Rule(new RegexMatcher('/class\\s+([a-z_]\\w+)/i'))]);
 }
Ejemplo n.º 10
0
 /**
  * Tokenization rules
  */
 public function setupRules()
 {
     $this->rules->addMany(['call.symbol' => new Rule(new RegexMatcher('/(\\\\[a-z]+)/si'), ['context' => Validator::everywhere(), 'priority' => -1]), 'string.math' => [new Rule(new RegexMatcher('/((\\${1,2}).*?\\2)/s')), new Rule(new RegexMatcher('/(\\\\\\(.*?\\\\\\))/s')), new Rule(new RegexMatcher('/(\\\\\\[.*?\\\\\\])/s')), new Rule(new RegexMatcher('/\\\\begin{((?:' . implode('|', self::$mathEnvironments) . ')\\*?)}(.*?)\\\\end{\\1}/s', [2 => Token::NAME]))], 'symbol.argument' => new Rule(new RegexMatcher('/\\[(.*?)\\]/')), 'symbol.environment' => new Rule(new RegexMatcher('/\\\\(?:begin|end){(.*?)}/')), 'symbol.label' => new Rule(new RegexMatcher('/\\\\(?:label|ref){(.*?)}/')), 'operator' => [new Rule(new WordMatcher(['*', '&', '\\\\'], ['separated' => false]), ['context' => Validator::everywhere()]), new Rule(new WordMatcher(['=', '-', '+', '/', '^', '_'], ['separated' => false]), ['context' => ['string.math'], 'priority' => -1])], 'comment' => new Rule(new CommentMatcher(['%'], [])), 'format.bold' => new Rule(new RegexMatcher('/\\\\textbf({((?>[^{}]+|(?1))+)})/si', [2 => Token::NAME])), 'format.italics' => new Rule(new RegexMatcher('/\\\\textit({((?>[^{}]+|(?1))+)})/si', [2 => Token::NAME])), 'number' => new Rule(new RegexMatcher('/(-?(?:0[0-7]+|0[xX][0-9a-fA-F]+|0b[01]+|[\\d,]+))/'), ['context' => ['string.math'], 'priority' => -2])]);
 }
Ejemplo n.º 11
0
 /**
  * Tokenization rules definition
  */
 public function setupRules()
 {
     $identifier = '\\w+';
     $number = '[+-]?(?=\\d|\\.\\d)\\d*(\\.\\d*)?([Ee]([+-]?\\d+))?';
     $this->rules->addMany(['string' => CommonFeatures::strings(['single' => '\'', 'double' => '"'], ['context' => ['!keyword', '!comment', '!string', '!language', '!number']]), 'comment' => new Rule(new CommentMatcher(['#'])), 'keyword' => new Rule(new WordMatcher(['case', 'continue', 'do', 'else', 'elsif', 'for', 'foreach', 'if', 'last', 'my', 'next', 'our', 'redo', 'reset', 'then', 'unless', 'until', 'while', 'use', 'print', 'new', 'BEGIN', 'sub', 'CHECK', 'INIT', 'END', 'return', 'exit'])), 'operator.escape' => new Rule(new RegexMatcher('/(\\\\.)/'), ['context' => ['string']]), 'string.nowdoc' => new Rule(new RegexMatcher('/<<\\s*\'(\\w+)\';(?P<string>.*?)\\R\\1/sm', ['string' => Token::NAME, 0 => 'keyword.nowdoc']), ['context' => ['!comment']]), 'language.shell' => new Rule(new SubStringMatcher('`'), ['context' => ['!operator.escape', '!comment', '!string', '!keyword.nowdoc'], 'factory' => new TokenFactory(ContextualToken::class)]), 'variable.scalar' => new Rule(new RegexMatcher("/(\\\${$identifier})/")), 'variable.array' => new Rule(new RegexMatcher("/(\\@{$identifier})/")), 'variable.hash' => new Rule(new RegexMatcher("/(\\%{$identifier})/")), 'variable.property' => new Rule(new RegexMatcher("/\\\${$identifier}{({$identifier})}/")), 'variable.special' => new Rule(new RegexMatcher('/([$@%][^\\s\\w]+[\\w]*)/')), 'operator' => [new Rule(new RegexMatcher('/(-[rwxoRWXOezsfdlpSbctugkTBMAC])/')), new Rule(new WordMatcher(['not', 'and', 'or', 'xor', 'goto', 'last', 'next', 'redo', 'dump', 'eq', 'ne', 'cmp', 'not', 'and', 'or', 'xor'], ['atomic' => true]))], 'call' => new Rule(new RegexMatcher('/([a-z]\\w+)(?:\\s*\\(|\\s+[$%@"\'`{])/i')), 'number' => [new Rule(new RegexMatcher("/(\\b|\"|'){$number}\\1/", [0 => Token::NAME]), ['priority' => 5])], 'string.regex' => [new OpenRule(new RegexMatcher('#~\\s*[ms]?(/).*?/#m'), ['context' => Validator::everywhere()]), new OpenRule(new RegexMatcher('#~\\s*(s/).*?/#m')), new Rule(new RegexMatcher('#(?=\\/.*?(/[gimuy]{0,5}))#m'), ['priority' => 1, 'factory' => new TokenFactory(ContextualToken::class), 'context' => ['!operator.escape', 'string.regex']])], 'symbol.iterator' => [new Rule(new RegexMatcher('#(<\\w+>)#s'))]]);
 }