ether-code 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cli/ether.js CHANGED
@@ -6,7 +6,7 @@ const http = require('http')
6
6
  const { EtherCompiler } = require('./compiler')
7
7
  const { Watcher } = require('./watcher')
8
8
 
9
- const VERSION = '0.4.0'
9
+ const VERSION = '0.4.1'
10
10
 
11
11
  const COLORS = {
12
12
  reset: '\x1b[0m',
package/ether-parser.js CHANGED
@@ -436,6 +436,7 @@ class EtherParser {
436
436
  }
437
437
 
438
438
  const valueParts = []
439
+ let lastType = null
439
440
 
440
441
  while (!this.isAtEnd()) {
441
442
  const current = this.current()
@@ -445,21 +446,31 @@ class EtherParser {
445
446
  break
446
447
  }
447
448
 
449
+ let needsSpace = valueParts.length > 0
450
+
451
+ if (current.type === TokenType.LPAREN || current.type === TokenType.COMMA ||
452
+ current.type === TokenType.RPAREN || current.type === TokenType.PERCENT ||
453
+ lastType === TokenType.LPAREN || lastType === TokenType.MINUS ||
454
+ lastType === TokenType.HASH || lastType === TokenType.COLON) {
455
+ needsSpace = false
456
+ }
457
+
448
458
  if (current.type === TokenType.IDENTIFIER) {
459
+ if (needsSpace) valueParts.push(' ')
449
460
  valueParts.push(current.value)
450
461
  } else if (current.type === TokenType.NUMBER || current.type === TokenType.INTEGER ||
451
462
  current.type === TokenType.FLOAT) {
463
+ if (needsSpace && lastType !== TokenType.HASH) valueParts.push(' ')
452
464
  valueParts.push(current.value)
453
465
  } else if (current.type === TokenType.STRING) {
466
+ if (needsSpace) valueParts.push(' ')
467
+ valueParts.push('"' + current.value + '"')
468
+ } else if (current.type === TokenType.HEX) {
469
+ if (needsSpace) valueParts.push(' ')
454
470
  valueParts.push(current.value)
455
- } else if (current.type === TokenType.HEX || current.type === TokenType.HASH) {
456
- const next = this.peek(1)
457
- if (next && (next.type === TokenType.IDENTIFIER || next.type === TokenType.NUMBER)) {
458
- this.advance()
459
- valueParts.push('#' + next.value)
460
- } else {
461
- valueParts.push('#')
462
- }
471
+ } else if (current.type === TokenType.HASH) {
472
+ if (needsSpace) valueParts.push(' ')
473
+ valueParts.push('#')
463
474
  } else if (current.type === TokenType.COMMA) {
464
475
  valueParts.push(',')
465
476
  } else if (current.type === TokenType.LPAREN) {
@@ -469,25 +480,33 @@ class EtherParser {
469
480
  } else if (current.type === TokenType.PERCENT) {
470
481
  valueParts.push('%')
471
482
  } else if (current.type === TokenType.SLASH) {
483
+ if (needsSpace) valueParts.push(' ')
472
484
  valueParts.push('/')
473
485
  } else if (current.type === TokenType.MINUS) {
474
- valueParts.push('-')
486
+ if (lastType === TokenType.LPAREN || lastType === TokenType.MINUS) {
487
+ valueParts.push('-')
488
+ } else if (needsSpace) {
489
+ valueParts.push(' -')
490
+ } else {
491
+ valueParts.push('-')
492
+ }
493
+ } else if (current.type === TokenType.COLON) {
494
+ valueParts.push(':')
495
+ } else if (current.type === TokenType.DOT) {
496
+ valueParts.push('.')
475
497
  } else {
476
498
  break
477
499
  }
478
500
 
501
+ lastType = current.type
479
502
  this.advance()
480
503
  }
481
504
 
482
505
  return {
483
506
  type: 'Declaration',
484
507
  property: property,
485
- value: valueParts.join(' ')
486
- .replace(/\(\s+/g, '(')
487
- .replace(/\s+\)/g, ')')
488
- .replace(/\s+,/g, ',')
489
- .replace(/-\s+/g, '-')
490
- .replace(/(\d+)\s+(%|px|em|rem|vh|vw|vmin|vmax|fr|s|ms|deg)/gi, '$1$2')
508
+ value: valueParts.join('')
509
+ .replace(/,\s*/g, ', ')
491
510
  .replace(/\s+/g, ' ')
492
511
  .trim()
493
512
  }
@@ -221,6 +221,11 @@ class EtherLexer {
221
221
  }
222
222
 
223
223
  if (char === '#') {
224
+ const next = this.peek(1)
225
+ if (next && (this.isHexDigit(next) || this.isAlpha(next))) {
226
+ this.scanHashToken()
227
+ return
228
+ }
224
229
  this.scanComment()
225
230
  return
226
231
  }
@@ -319,6 +324,42 @@ class EtherLexer {
319
324
  }
320
325
  }
321
326
 
327
+ scanHashToken() {
328
+ const startLine = this.line
329
+ const startCol = this.column
330
+
331
+ this.advance()
332
+
333
+ let value = ''
334
+ let isHexColor = true
335
+ let charCount = 0
336
+
337
+ while (!this.isAtEnd()) {
338
+ const char = this.peek()
339
+ if (this.isHexDigit(char)) {
340
+ value += this.advance()
341
+ charCount++
342
+ } else if (this.isAlpha(char) && !this.isHexDigit(char)) {
343
+ isHexColor = false
344
+ value += this.advance()
345
+ } else if (char === '-' || char === '_') {
346
+ isHexColor = false
347
+ value += this.advance()
348
+ } else {
349
+ break
350
+ }
351
+ }
352
+
353
+ if (isHexColor && (charCount === 3 || charCount === 4 || charCount === 6 || charCount === 8)) {
354
+ this.tokens.push(new Token(TokenType.HEX, '#' + value, startLine, startCol, this.currentIndent))
355
+ } else {
356
+ this.tokens.push(new Token(TokenType.HASH, '#', startLine, startCol, this.currentIndent))
357
+ if (value) {
358
+ this.tokens.push(new Token(TokenType.IDENTIFIER, value, startLine, startCol + 1, this.currentIndent))
359
+ }
360
+ }
361
+ }
362
+
322
363
  scanBlockComment() {
323
364
  const startLine = this.line
324
365
  const startCol = this.column
@@ -521,10 +562,42 @@ class EtherLexer {
521
562
  }
522
563
 
523
564
  if (type === TokenType.FLOAT) {
524
- this.tokens.push(new Token(type, parseFloat(value), startLine, startCol, this.currentIndent))
565
+ let numValue = String(parseFloat(value))
566
+ const unit = this.scanCSSUnit()
567
+ if (unit) numValue += unit
568
+ this.tokens.push(new Token(type, numValue, startLine, startCol, this.currentIndent))
525
569
  } else {
526
- this.tokens.push(new Token(type, parseInt(value, 10), startLine, startCol, this.currentIndent))
570
+ let numValue = String(parseInt(value, 10))
571
+ const unit = this.scanCSSUnit()
572
+ if (unit) numValue += unit
573
+ this.tokens.push(new Token(type, numValue, startLine, startCol, this.currentIndent))
574
+ }
575
+ }
576
+
577
+ scanCSSUnit() {
578
+ const units = ['px', 'em', 'rem', 'vh', 'vw', 'vmin', 'vmax', '%', 'fr', 's', 'ms', 'deg', 'rad', 'turn', 'ch', 'ex', 'cm', 'mm', 'in', 'pt', 'pc', 'cqw', 'cqh']
579
+
580
+ for (const unit of units) {
581
+ let matches = true
582
+ for (let i = 0; i < unit.length; i++) {
583
+ const char = this.peek(i)
584
+ if (!char || char.toLowerCase() !== unit[i].toLowerCase()) {
585
+ matches = false
586
+ break
587
+ }
588
+ }
589
+ if (matches) {
590
+ const afterUnit = this.peek(unit.length)
591
+ if (!afterUnit || !this.isAlpha(afterUnit)) {
592
+ let result = ''
593
+ for (let i = 0; i < unit.length; i++) {
594
+ result += this.advance()
595
+ }
596
+ return result
597
+ }
598
+ }
527
599
  }
600
+ return ''
528
601
  }
529
602
 
530
603
  scanIdentifier() {
@@ -693,7 +766,15 @@ class EtherLexer {
693
766
 
694
767
  case '-':
695
768
  if (this.match('-')) {
696
- this.tokens.push(new Token(TokenType.MINUS, '--', startLine, startCol, this.currentIndent))
769
+ if (this.isAlpha(this.peek()) || this.peek() === '-') {
770
+ let varName = '--'
771
+ while (this.isAlphaNumeric(this.peek()) || this.peek() === '-' || this.peek() === '_') {
772
+ varName += this.advance()
773
+ }
774
+ this.tokens.push(new Token(TokenType.IDENTIFIER, varName, startLine, startCol, this.currentIndent))
775
+ } else {
776
+ this.tokens.push(new Token(TokenType.MINUS, '--', startLine, startCol, this.currentIndent))
777
+ }
697
778
  } else if (this.match('>')) {
698
779
  this.tokens.push(new Token(TokenType.ARROW, '->', startLine, startCol, this.currentIndent))
699
780
  } else if (this.match('=')) {
@@ -866,4 +947,4 @@ module.exports = {
866
947
  Token,
867
948
  TokenType,
868
949
  LANG_BLOCKS
869
- }
950
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ether-code",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "Ether - Le langage intentionnel",
5
5
  "main": "cli/compiler.js",
6
6
  "bin": {