-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.php
2627 lines (2112 loc) · 59.7 KB
/
Parser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
require_once( dirname(__FILE__).'/Cache.php');
/**
* Class for parsing and compiling less files into css
*
* @package Less
* @subpackage parser
*
*/
class Less_Parser{
/**
* Default parser options
*/
public static $default_options = array(
'compress' => false, // option - whether to compress
'strictUnits' => false, // whether units need to evaluate correctly
'strictMath' => false, // whether math has to be within parenthesis
'relativeUrls' => true, // option - whether to adjust URL's to be relative
'urlArgs' => array(), // whether to add args into url tokens
'numPrecision' => 8,
'import_dirs' => array(),
'import_callback' => null,
'cache_dir' => null,
'cache_method' => 'php', // false, 'serialize', 'php', 'var_export', 'callback';
'cache_callback_get' => null,
'cache_callback_set' => null,
'sourceMap' => false, // whether to output a source map
'sourceMapBasepath' => null,
'sourceMapWriteTo' => null,
'sourceMapURL' => null,
'plugins' => array(),
);
public static $options = array();
private $input; // Less input string
private $input_len; // input string length
private $pos; // current index in `input`
private $saveStack = array(); // holds state for backtracking
private $furthest;
private $mb_internal_encoding = ''; // for remember exists value of mbstring.internal_encoding
/**
* @var Less_Environment
*/
private $env;
protected $rules = array();
private static $imports = array();
public static $has_extends = false;
public static $next_id = 0;
/**
* Filename to contents of all parsed the files
*
* @var array
*/
public static $contentsMap = array();
/**
* @param Less_Environment|array|null $env
*/
public function __construct( $env = null ){
// Top parser on an import tree must be sure there is one "env"
// which will then be passed around by reference.
if( $env instanceof Less_Environment ){
$this->env = $env;
}else{
$this->SetOptions(Less_Parser::$default_options);
$this->Reset( $env );
}
// mbstring.func_overload > 1 bugfix
// The encoding value must be set for each source file,
// therefore, to conserve resources and improve the speed of this design is taken here
if (ini_get('mbstring.func_overload')) {
$this->mb_internal_encoding = ini_get('mbstring.internal_encoding');
@ini_set('mbstring.internal_encoding', 'ascii');
}
}
/**
* Reset the parser state completely
*
*/
public function Reset( $options = null ){
$this->rules = array();
self::$imports = array();
self::$has_extends = false;
self::$imports = array();
self::$contentsMap = array();
$this->env = new Less_Environment($options);
$this->env->Init();
//set new options
if( is_array($options) ){
$this->SetOptions(Less_Parser::$default_options);
$this->SetOptions($options);
}
}
/**
* Set one or more compiler options
* options: import_dirs, cache_dir, cache_method
*
*/
public function SetOptions( $options ){
foreach($options as $option => $value){
$this->SetOption($option,$value);
}
}
/**
* Set one compiler option
*
*/
public function SetOption($option,$value){
switch($option){
case 'import_dirs':
$this->SetImportDirs($value);
return;
case 'cache_dir':
if( is_string($value) ){
Less_Cache::SetCacheDir($value);
Less_Cache::CheckCacheDir();
}
return;
}
Less_Parser::$options[$option] = $value;
}
/**
* Registers a new custom function
*
* @param string $name function name
* @param callable $callback callback
*/
public function registerFunction($name, $callback) {
$this->env->functions[$name] = $callback;
}
/**
* Removed an already registered function
*
* @param string $name function name
*/
public function unregisterFunction($name) {
if( isset($this->env->functions[$name]) )
unset($this->env->functions[$name]);
}
/**
* Get the current css buffer
*
* @return string
*/
public function getCss(){
$precision = ini_get('precision');
@ini_set('precision',16);
$locale = setlocale(LC_NUMERIC, 0);
setlocale(LC_NUMERIC, "C");
try {
$root = new Less_Tree_Ruleset(array(), $this->rules );
$root->root = true;
$root->firstRoot = true;
$this->PreVisitors($root);
self::$has_extends = false;
$evaldRoot = $root->compile($this->env);
$this->PostVisitors($evaldRoot);
if( Less_Parser::$options['sourceMap'] ){
$generator = new Less_SourceMap_Generator($evaldRoot, Less_Parser::$contentsMap, Less_Parser::$options );
// will also save file
// FIXME: should happen somewhere else?
$css = $generator->generateCSS();
}else{
$css = $evaldRoot->toCSS();
}
if( Less_Parser::$options['compress'] ){
$css = preg_replace('/(^(\s)+)|((\s)+$)/', '', $css);
}
} catch (Exception $exc) {
// Intentional fall-through so we can reset environment
}
//reset php settings
@ini_set('precision',$precision);
setlocale(LC_NUMERIC, $locale);
// If you previously defined $this->mb_internal_encoding
// is required to return the encoding as it was before
if ($this->mb_internal_encoding != '') {
@ini_set("mbstring.internal_encoding", $this->mb_internal_encoding);
$this->mb_internal_encoding = '';
}
// Rethrow exception after we handled resetting the environment
if (!empty($exc)) {
throw $exc;
}
return $css;
}
/**
* Run pre-compile visitors
*
*/
private function PreVisitors($root){
if( Less_Parser::$options['plugins'] ){
foreach(Less_Parser::$options['plugins'] as $plugin){
if( !empty($plugin->isPreEvalVisitor) ){
$plugin->run($root);
}
}
}
}
/**
* Run post-compile visitors
*
*/
private function PostVisitors($evaldRoot){
$visitors = array();
$visitors[] = new Less_Visitor_joinSelector();
if( self::$has_extends ){
$visitors[] = new Less_Visitor_processExtends();
}
$visitors[] = new Less_Visitor_toCSS();
if( Less_Parser::$options['plugins'] ){
foreach(Less_Parser::$options['plugins'] as $plugin){
if( property_exists($plugin,'isPreEvalVisitor') && $plugin->isPreEvalVisitor ){
continue;
}
if( property_exists($plugin,'isPreVisitor') && $plugin->isPreVisitor ){
array_unshift( $visitors, $plugin);
}else{
$visitors[] = $plugin;
}
}
}
for($i = 0; $i < count($visitors); $i++ ){
$visitors[$i]->run($evaldRoot);
}
}
/**
* Parse a Less string into css
*
* @param string $str The string to convert
* @param string $uri_root The url of the file
* @return Less_Tree_Ruleset|Less_Parser
*/
public function parse( $str, $file_uri = null ){
if( !$file_uri ){
$uri_root = '';
$filename = 'anonymous-file-'.Less_Parser::$next_id++.'.less';
}else{
$file_uri = self::WinPath($file_uri);
$filename = $file_uri;
$uri_root = dirname($file_uri);
}
$previousFileInfo = $this->env->currentFileInfo;
$uri_root = self::WinPath($uri_root);
$this->SetFileInfo($filename, $uri_root);
$this->input = $str;
$this->_parse();
if( $previousFileInfo ){
$this->env->currentFileInfo = $previousFileInfo;
}
return $this;
}
/**
* Parse a Less string from a given file
*
* @throws Less_Exception_Parser
* @param string $filename The file to parse
* @param string $uri_root The url of the file
* @param bool $returnRoot Indicates whether the return value should be a css string a root node
* @return Less_Tree_Ruleset|Less_Parser
*/
public function parseFile( $filename, $uri_root = '', $returnRoot = false){
if( !file_exists($filename) ){
$this->Error(sprintf('File `%s` not found.', $filename));
}
// fix uri_root?
// Instead of The mixture of file path for the first argument and directory path for the second argument has bee
if( !$returnRoot && !empty($uri_root) && basename($uri_root) == basename($filename) ){
$uri_root = dirname($uri_root);
}
$previousFileInfo = $this->env->currentFileInfo;
if( $filename ){
$filename = self::WinPath(realpath($filename));
}
$uri_root = self::WinPath($uri_root);
$this->SetFileInfo($filename, $uri_root);
self::AddParsedFile($filename);
if( $returnRoot ){
$rules = $this->GetRules( $filename );
$return = new Less_Tree_Ruleset(array(), $rules );
}else{
$this->_parse( $filename );
$return = $this;
}
if( $previousFileInfo ){
$this->env->currentFileInfo = $previousFileInfo;
}
return $return;
}
/**
* Allows a user to set variables values
* @param array $vars
* @return Less_Parser
*/
public function ModifyVars( $vars ){
$this->input = Less_Parser::serializeVars( $vars );
$this->_parse();
return $this;
}
/**
* @param string $filename
*/
public function SetFileInfo( $filename, $uri_root = ''){
$filename = Less_Environment::normalizePath($filename);
$dirname = preg_replace('/[^\/\\\\]*$/','',$filename);
if( !empty($uri_root) ){
$uri_root = rtrim($uri_root,'/').'/';
}
$currentFileInfo = array();
//entry info
if( isset($this->env->currentFileInfo) ){
$currentFileInfo['entryPath'] = $this->env->currentFileInfo['entryPath'];
$currentFileInfo['entryUri'] = $this->env->currentFileInfo['entryUri'];
$currentFileInfo['rootpath'] = $this->env->currentFileInfo['rootpath'];
}else{
$currentFileInfo['entryPath'] = $dirname;
$currentFileInfo['entryUri'] = $uri_root;
$currentFileInfo['rootpath'] = $dirname;
}
$currentFileInfo['currentDirectory'] = $dirname;
$currentFileInfo['currentUri'] = $uri_root.basename($filename);
$currentFileInfo['filename'] = $filename;
$currentFileInfo['uri_root'] = $uri_root;
//inherit reference
if( isset($this->env->currentFileInfo['reference']) && $this->env->currentFileInfo['reference'] ){
$currentFileInfo['reference'] = true;
}
$this->env->currentFileInfo = $currentFileInfo;
}
/**
* @deprecated 1.5.1.2
*
*/
public function SetCacheDir( $dir ){
if( !file_exists($dir) ){
if( mkdir($dir) ){
return true;
}
throw new Less_Exception_Parser('Less.php cache directory couldn\'t be created: '.$dir);
}elseif( !is_dir($dir) ){
throw new Less_Exception_Parser('Less.php cache directory doesn\'t exist: '.$dir);
}elseif( !is_writable($dir) ){
throw new Less_Exception_Parser('Less.php cache directory isn\'t writable: '.$dir);
}else{
$dir = self::WinPath($dir);
Less_Cache::$cache_dir = rtrim($dir,'/').'/';
return true;
}
}
/**
* Set a list of directories or callbacks the parser should use for determining import paths
*
* @param array $dirs
*/
public function SetImportDirs( $dirs ){
Less_Parser::$options['import_dirs'] = array();
foreach($dirs as $path => $uri_root){
$path = self::WinPath($path);
if( !empty($path) ){
$path = rtrim($path,'/').'/';
}
if ( !is_callable($uri_root) ){
$uri_root = self::WinPath($uri_root);
if( !empty($uri_root) ){
$uri_root = rtrim($uri_root,'/').'/';
}
}
Less_Parser::$options['import_dirs'][$path] = $uri_root;
}
}
/**
* @param string $file_path
*/
private function _parse( $file_path = null ){
$this->rules = array_merge($this->rules, $this->GetRules( $file_path ));
}
/**
* Return the results of parsePrimary for $file_path
* Use cache and save cached results if possible
*
* @param string|null $file_path
*/
private function GetRules( $file_path ){
$this->SetInput($file_path);
$cache_file = $this->CacheFile( $file_path );
if( $cache_file ){
if( Less_Parser::$options['cache_method'] == 'callback' ){
if( is_callable(Less_Parser::$options['cache_callback_get']) ){
$cache = call_user_func_array(
Less_Parser::$options['cache_callback_get'],
array($this, $file_path, $cache_file)
);
if( $cache ){
$this->UnsetInput();
return $cache;
}
}
}elseif( file_exists($cache_file) ){
switch(Less_Parser::$options['cache_method']){
// Using serialize
// Faster but uses more memory
case 'serialize':
$cache = unserialize(file_get_contents($cache_file));
if( $cache ){
touch($cache_file);
$this->UnsetInput();
return $cache;
}
break;
// Using generated php code
case 'var_export':
case 'php':
$this->UnsetInput();
return include($cache_file);
}
}
}
$rules = $this->parsePrimary();
if( $this->pos < $this->input_len ){
throw new Less_Exception_Chunk($this->input, null, $this->furthest, $this->env->currentFileInfo);
}
$this->UnsetInput();
//save the cache
if( $cache_file ){
if( Less_Parser::$options['cache_method'] == 'callback' ){
if( is_callable(Less_Parser::$options['cache_callback_set']) ){
call_user_func_array(
Less_Parser::$options['cache_callback_set'],
array($this, $file_path, $cache_file, $rules)
);
}
}else{
//msg('write cache file');
switch(Less_Parser::$options['cache_method']){
case 'serialize':
file_put_contents( $cache_file, serialize($rules) );
break;
case 'php':
file_put_contents( $cache_file, '<?php return '.self::ArgString($rules).'; ?>' );
break;
case 'var_export':
//Requires __set_state()
file_put_contents( $cache_file, '<?php return '.var_export($rules,true).'; ?>' );
break;
}
Less_Cache::CleanCache();
}
}
return $rules;
}
/**
* Set up the input buffer
*
*/
public function SetInput( $file_path ){
if( $file_path ){
$this->input = file_get_contents( $file_path );
}
$this->pos = $this->furthest = 0;
// Remove potential UTF Byte Order Mark
$this->input = preg_replace('/\\G\xEF\xBB\xBF/', '', $this->input);
$this->input_len = strlen($this->input);
if( Less_Parser::$options['sourceMap'] && $this->env->currentFileInfo ){
$uri = $this->env->currentFileInfo['currentUri'];
Less_Parser::$contentsMap[$uri] = $this->input;
}
}
/**
* Free up some memory
*
*/
public function UnsetInput(){
unset($this->input, $this->pos, $this->input_len, $this->furthest);
$this->saveStack = array();
}
public function CacheFile( $file_path ){
if( $file_path && $this->CacheEnabled() ){
$env = get_object_vars($this->env);
unset($env['frames']);
$parts = array();
$parts[] = $file_path;
$parts[] = filesize( $file_path );
$parts[] = filemtime( $file_path );
$parts[] = $env;
$parts[] = Less_Version::cache_version;
$parts[] = Less_Parser::$options['cache_method'];
return Less_Cache::$cache_dir . Less_Cache::$prefix . base_convert( sha1(json_encode($parts) ), 16, 36) . '.lesscache';
}
}
static function AddParsedFile($file){
self::$imports[] = $file;
}
static function AllParsedFiles(){
return self::$imports;
}
/**
* @param string $file
*/
static function FileParsed($file){
return in_array($file,self::$imports);
}
function save() {
$this->saveStack[] = $this->pos;
}
private function restore() {
$this->pos = array_pop($this->saveStack);
}
private function forget(){
array_pop($this->saveStack);
}
private function isWhitespace($offset = 0) {
return preg_match('/\s/',$this->input[ $this->pos + $offset]);
}
/**
* Parse from a token, regexp or string, and move forward if match
*
* @param array $toks
* @return array
*/
private function match($toks){
// The match is confirmed, add the match length to `this::pos`,
// and consume any extra white-space characters (' ' || '\n')
// which come after that. The reason for this is that LeSS's
// grammar is mostly white-space insensitive.
//
foreach($toks as $tok){
$char = $tok[0];
if( $char === '/' ){
$match = $this->MatchReg($tok);
if( $match ){
return count($match) === 1 ? $match[0] : $match;
}
}elseif( $char === '#' ){
$match = $this->MatchChar($tok[1]);
}else{
// Non-terminal, match using a function call
$match = $this->$tok();
}
if( $match ){
return $match;
}
}
}
/**
* @param string[] $toks
*
* @return string
*/
private function MatchFuncs($toks){
if( $this->pos < $this->input_len ){
foreach($toks as $tok){
$match = $this->$tok();
if( $match ){
return $match;
}
}
}
}
// Match a single character in the input,
private function MatchChar($tok){
if( ($this->pos < $this->input_len) && ($this->input[$this->pos] === $tok) ){
$this->skipWhitespace(1);
return $tok;
}
}
// Match a regexp from the current start point
private function MatchReg($tok){
if( preg_match($tok, $this->input, $match, 0, $this->pos) ){
$this->skipWhitespace(strlen($match[0]));
return $match;
}
}
/**
* Same as match(), but don't change the state of the parser,
* just return the match.
*
* @param string $tok
* @return integer
*/
public function PeekReg($tok){
return preg_match($tok, $this->input, $match, 0, $this->pos);
}
/**
* @param string $tok
*/
public function PeekChar($tok){
//return ($this->input[$this->pos] === $tok );
return ($this->pos < $this->input_len) && ($this->input[$this->pos] === $tok );
}
/**
* @param integer $length
*/
public function skipWhitespace($length){
$this->pos += $length;
for(; $this->pos < $this->input_len; $this->pos++ ){
$c = $this->input[$this->pos];
if( ($c !== "\n") && ($c !== "\r") && ($c !== "\t") && ($c !== ' ') ){
break;
}
}
}
/**
* @param string $tok
* @param string|null $msg
*/
public function expect($tok, $msg = NULL) {
$result = $this->match( array($tok) );
if (!$result) {
$this->Error( $msg ? "Expected '" . $tok . "' got '" . $this->input[$this->pos] . "'" : $msg );
} else {
return $result;
}
}
/**
* @param string $tok
*/
public function expectChar($tok, $msg = null ){
$result = $this->MatchChar($tok);
if( !$result ){
$this->Error( $msg ? "Expected '" . $tok . "' got '" . $this->input[$this->pos] . "'" : $msg );
}else{
return $result;
}
}
//
// Here in, the parsing rules/functions
//
// The basic structure of the syntax tree generated is as follows:
//
// Ruleset -> Rule -> Value -> Expression -> Entity
//
// Here's some LESS code:
//
// .class {
// color: #fff;
// border: 1px solid #000;
// width: @w + 4px;
// > .child {...}
// }
//
// And here's what the parse tree might look like:
//
// Ruleset (Selector '.class', [
// Rule ("color", Value ([Expression [Color #fff]]))
// Rule ("border", Value ([Expression [Dimension 1px][Keyword "solid"][Color #000]]))
// Rule ("width", Value ([Expression [Operation "+" [Variable "@w"][Dimension 4px]]]))
// Ruleset (Selector [Element '>', '.child'], [...])
// ])
//
// In general, most rules will try to parse a token with the `$()` function, and if the return
// value is truly, will return a new node, of the relevant type. Sometimes, we need to check
// first, before parsing, that's when we use `peek()`.
//
//
// The `primary` rule is the *entry* and *exit* point of the parser.
// The rules here can appear at any level of the parse tree.
//
// The recursive nature of the grammar is an interplay between the `block`
// rule, which represents `{ ... }`, the `ruleset` rule, and this `primary` rule,
// as represented by this simplified grammar:
//
// primary → (ruleset | rule)+
// ruleset → selector+ block
// block → '{' primary '}'
//
// Only at one point is the primary rule not called from the
// block rule: at the root level.
//
private function parsePrimary(){
$root = array();
while( true ){
if( $this->pos >= $this->input_len ){
break;
}
$node = $this->parseExtend(true);
if( $node ){
$root = array_merge($root,$node);
continue;
}
//$node = $this->MatchFuncs( array( 'parseMixinDefinition', 'parseRule', 'parseRuleset', 'parseMixinCall', 'parseComment', 'parseDirective'));
$node = $this->MatchFuncs( array( 'parseMixinDefinition', 'parseNameValue', 'parseRule', 'parseRuleset', 'parseMixinCall', 'parseComment', 'parseRulesetCall', 'parseDirective'));
if( $node ){
$root[] = $node;
}elseif( !$this->MatchReg('/\\G[\s\n;]+/') ){
break;
}
if( $this->PeekChar('}') ){
break;
}
}
return $root;
}
// We create a Comment node for CSS comments `/* */`,
// but keep the LeSS comments `//` silent, by just skipping
// over them.
private function parseComment(){
if( $this->input[$this->pos] !== '/' ){
return;
}
if( $this->input[$this->pos+1] === '/' ){
$match = $this->MatchReg('/\\G\/\/.*/');
return $this->NewObj4('Less_Tree_Comment',array($match[0], true, $this->pos, $this->env->currentFileInfo));
}
//$comment = $this->MatchReg('/\\G\/\*(?:[^*]|\*+[^\/*])*\*+\/\n?/');
$comment = $this->MatchReg('/\\G\/\*(?s).*?\*+\/\n?/');//not the same as less.js to prevent fatal errors
if( $comment ){
return $this->NewObj4('Less_Tree_Comment',array($comment[0], false, $this->pos, $this->env->currentFileInfo));
}
}
private function parseComments(){
$comments = array();
while( $this->pos < $this->input_len ){
$comment = $this->parseComment();
if( !$comment ){
break;
}
$comments[] = $comment;
}
return $comments;
}
//
// A string, which supports escaping " and '
//
// "milky way" 'he\'s the one!'
//
private function parseEntitiesQuoted() {
$j = $this->pos;
$e = false;
$index = $this->pos;
if( $this->input[$this->pos] === '~' ){
$j++;
$e = true; // Escaped strings
}
if( $this->input[$j] != '"' && $this->input[$j] !== "'" ){
return;
}
if ($e) {
$this->MatchChar('~');
}
// Fix for #124: match escaped newlines
//$str = $this->MatchReg('/\\G"((?:[^"\\\\\r\n]|\\\\.)*)"|\'((?:[^\'\\\\\r\n]|\\\\.)*)\'/');
$str = $this->MatchReg('/\\G"((?:[^"\\\\\r\n]|\\\\.|\\\\\r\n|\\\\[\n\r\f])*)"|\'((?:[^\'\\\\\r\n]|\\\\.|\\\\\r\n|\\\\[\n\r\f])*)\'/');
if( $str ){
$result = $str[0][0] == '"' ? $str[1] : $str[2];
return $this->NewObj5('Less_Tree_Quoted',array($str[0], $result, $e, $index, $this->env->currentFileInfo) );
}
return;
}
//
// A catch-all word, such as:
//
// black border-collapse
//
private function parseEntitiesKeyword(){
//$k = $this->MatchReg('/\\G[_A-Za-z-][_A-Za-z0-9-]*/');
$k = $this->MatchReg('/\\G%|\\G[_A-Za-z-][_A-Za-z0-9-]*/');
if( $k ){
$k = $k[0];
$color = $this->fromKeyword($k);
if( $color ){
return $color;
}
return $this->NewObj1('Less_Tree_Keyword',$k);
}
}
// duplicate of Less_Tree_Color::FromKeyword
private function FromKeyword( $keyword ){
$keyword = strtolower($keyword);
if( Less_Colors::hasOwnProperty($keyword) ){
// detect named color
return $this->NewObj1('Less_Tree_Color',substr(Less_Colors::color($keyword), 1));
}
if( $keyword === 'transparent' ){
return $this->NewObj3('Less_Tree_Color', array( array(0, 0, 0), 0, true));
}
}
//
// A function call
//
// rgb(255, 0, 255)
//
// We also try to catch IE's `alpha()`, but let the `alpha` parser
// deal with the details.
//
// The arguments are parsed with the `entities.arguments` parser.