Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Floating number with unknown unit can't be parsed #132

Open
perqin opened this issue Apr 26, 2021 · 3 comments · May be fixed by #164
Open

Floating number with unknown unit can't be parsed #132

perqin opened this issue Apr 26, 2021 · 3 comments · May be fixed by #164
Labels
bad-parser postcss-calc fails to parse the input bug

Comments

@perqin
Copy link

perqin commented Apr 26, 2021

postcss-calc leaves integer with unknown unit untouched, but when it comes to floating number, it fails.

// dependencies
var fs = require("fs")
var postcss = require("postcss")
var calc = require("postcss-calc")

var css = 'body { height: calc(120rpx - 41.7rpx); }';

console.log(postcss().use(calc()).process('body { height: calc(120rpx - 41rpx); }').css);
console.log('Passed test 1');

// process css
var output = postcss()
  .use(calc())
  .process(css)
  .css

console.log('output: ', output);
console.log('Passed test 2');

Here's the error log:

/Users/perqinxie/Workspaces/_demo/postcss-calc-issue/node_modules/postcss/lib/lazy-result.js:288
        throw this.handleError(e, node.proxyOf)
        ^
JisonParserError: Parse error on line 1:
120rpx - 41.7rpx
-----------^
Expecting end of input, "RPAREN", "ADD", "SUB", "MUL", "DIV", got unexpected "UNKNOWN_DIMENSION"
    at <input css XMfWJ3>:1:1
    at <input css XMfWJ3>:1:8
    at Parser.parseError (/Users/perqinxie/Workspaces/_demo/postcss-calc-issue/node_modules/postcss-calc/dist/parser.js:1200:15)
    at Parser.parse (/Users/perqinxie/Workspaces/_demo/postcss-calc-issue/node_modules/postcss-calc/dist/parser.js:1716:30)
    at /Users/perqinxie/Workspaces/_demo/postcss-calc-issue/node_modules/postcss-calc/dist/lib/transform.js:33:32
    at walk (/Users/perqinxie/Workspaces/_demo/postcss-calc-issue/node_modules/postcss-value-parser/lib/walk.js:7:16)
    at ValueParser.walk (/Users/perqinxie/Workspaces/_demo/postcss-calc-issue/node_modules/postcss-value-parser/lib/index.js:18:3)
    at transformValue (/Users/perqinxie/Workspaces/_demo/postcss-calc-issue/node_modules/postcss-calc/dist/lib/transform.js:24:50)
    at _default (/Users/perqinxie/Workspaces/_demo/postcss-calc-issue/node_modules/postcss-calc/dist/lib/transform.js:66:102)
    at /Users/perqinxie/Workspaces/_demo/postcss-calc-issue/node_modules/postcss-calc/dist/index.js:32:34
    at /Users/perqinxie/Workspaces/_demo/postcss-calc-issue/node_modules/postcss/lib/container.js:74:18
    at Rule.each (/Users/perqinxie/Workspaces/_demo/postcss-calc-issue/node_modules/postcss/lib/container.js:60:16) {
  hash: {
    errStr: 'Parse error on line 1: \n' +
      '120rpx - 41.7rpx\n' +
      '-----------^\n' +
      'Expecting end of input, "RPAREN", "ADD", "SUB", "MUL", "DIV", got unexpected "UNKNOWN_DIMENSION"',
    exception: null,
    text: '.7rpx',
    value: '.7rpx',
    token: '"UNKNOWN_DIMENSION"',
    token_id: 16,
    line: 0,
    expected: [ 'end of input', '"RPAREN"', '"ADD"', '"SUB"', '"MUL"', '"DIV"' ],
    recoverable: false,
    state: 41,
    action: 0,
    new_state: 47,
    symbol_stack: [],
    state_stack: [],
    value_stack: [],
    stack_pointer: 4,
    yy: {
      parseError: undefined,
      quoteName: undefined,
      lexer: undefined,
      parser: undefined,
      pre_parse: undefined,
      post_parse: undefined,
      pre_lex: undefined,
      post_lex: undefined
    },
    lexer: {
      yy: {},
      __decompressed: true,
      _input: '',
      yytext: '',
      yyleng: 0,
      match: '',
      matches: false,
      _more: false,
      _backtrack: false,
      yylloc: {
        first_line: 1,
        first_column: 0,
        last_line: 1,
        last_column: 0,
        range: [ 0, 0 ]
      },
      _signaled_error_token: false,
      done: false,
      yylineno: 0,
      matched: '',
      conditionStack: [ 'INITIAL' ],
      __currentRuleSet__: null,
      offset: 0
    },
    parser: {
      yy: {},
      __lexer__: {
        yy: {},
        __decompressed: true,
        _input: '',
        yytext: '',
        yyleng: 0,
        match: '',
        matches: false,
        _more: false,
        _backtrack: false,
        yylloc: {
          first_line: 1,
          first_column: 0,
          last_line: 1,
          last_column: 0,
          range: [ 0, 0 ]
        },
        _signaled_error_token: false,
        done: false,
        yylineno: 0,
        matched: '',
        conditionStack: [ 'INITIAL' ],
        __currentRuleSet__: null,
        offset: 0
      },
      yyGetSharedState: [Function: yyGetSharedState],
      parseError: [Function: parseError],
      quoteName: [Function: parser_quoteName],
      cleanupAfterParse: [Function: parser_cleanupAfterParse],
      constructParseErrorInfo: [Function: parser_constructParseErrorInfo],
      __reentrant_call_depth: 0
    },
    destroy: [Function: destructParseErrorInfo]
  },
  postcssNode: <ref *2> Root {
    raws: { semicolon: false, after: '' },
    type: 'root',
    nodes: [
      <ref *1> Rule {
        raws: { before: '', between: ' ', semicolon: true, after: ' ' },
        type: 'rule',
        nodes: [
          Declaration {
            raws: [Object],
            type: 'decl',
            parent: [Circular *1],
            source: [Object],
            prop: 'height',
            value: 'calc(120rpx - 41.7rpx)',
            [Symbol(isClean)]: true
          }
        ],
        parent: [Circular *2],
        source: {
          start: { offset: 0, line: 1, column: 1 },
          input: Input {
            css: 'body { height: calc(120rpx - 41.7rpx); }',
            hasBOM: false,
            id: '<input css XMfWJ3>',
            [Symbol(fromOffset cache)]: [Array]
          },
          end: { offset: 39, line: 1, column: 40 }
        },
        selector: 'body',
        lastEach: 2,
        indexes: { '2': 0 },
        [Symbol(isClean)]: true
      }
    ],
    source: {
      input: Input {
        css: 'body { height: calc(120rpx - 41.7rpx); }',
        hasBOM: false,
        id: '<input css XMfWJ3>',
        [Symbol(fromOffset cache)]: [ 0 ]
      },
      start: { offset: 0, line: 1, column: 1 }
    },
    lastEach: 2,
    indexes: { '2': 0 },
    [Symbol(isClean)]: true
  }
}
@ludofischer
Copy link
Collaborator

In 8.2, it still fails to parse, but at least does not crash.

@ludofischer ludofischer added bad-parser postcss-calc fails to parse the input bug labels Jan 9, 2022
@ludofischer
Copy link
Collaborator

Moving the UNKNOWN_DIMENSION before NUMBER in the lexer solves this issue, but then it prevents numbers with exponents to be reduced.

ludofischer added a commit that referenced this issue Jan 12, 2022
Fix #132

`\b` also matches '.' so the regex for the number expression would match
`41` in `41.7rpx`. Then `UNKNOWN_DIMENSION` would match `.7rpx`
and create the invalid sequence `NumberExpression`, `UnknownDimension`.
ludofischer added a commit that referenced this issue Jan 12, 2022
Fix #132

`\b` also matches '.', so the regex for the number expression would match
`41` in `41.7rpx`. Then `UNKNOWN_DIMENSION` would match `.7rpx`
and create the invalid sequence `NumberExpression`, `UnknownDimension`.
@tutuxxx
Copy link

tutuxxx commented Jul 4, 2023

same problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bad-parser postcss-calc fails to parse the input bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants