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

Question: should the expression (10 -23) throw syntax error? #1225

Closed
SomMeri opened this issue Mar 14, 2013 · 12 comments
Closed

Question: should the expression (10 -23) throw syntax error? #1225

SomMeri opened this issue Mar 14, 2013 · 12 comments

Comments

@SomMeri
Copy link
Member

SomMeri commented Mar 14, 2013

Full code:

test-works {
  something: (10 -23); 
}

Error:

SyntaxError: expected ')' got '-' in c:\...\test.less on line 2, column 18:
1 test-works {
2   something: (10 -23);
3 }

It is probably related to this: #952

Tested on 1.4.0-b1 with windows node.js.

@jonschlinkert
Copy link
Contributor

Interesting, it doesn't throw an error if you add a space on both side of the minus sign:

test-works {
  something: (10 - 23); 
}

but I confirmed that it does throw an error as you've written it

@lukeapage
Copy link
Member

yes, it is interpreted as 10 and -23, two values in a space separated list, inside brackets.

At the moment the interpretation of brackets happens after parsing in less.js

@lukeapage
Copy link
Member

.. therefore to fix it we would have to allow (12 (0 - 1)) to be valid or do something more complicated. I don't think it is a hardship to ask the user to clarify what they mean.

@matthew-dean
Copy link
Member

But in parentheses, doesn't that force attempt of math?

@matthew-dean
Copy link
Member

That is, is there a a valid value that has parentheses as the value (that isn't clearly a media block or a url or a registered LESS function) that wouldn't be math? If not, seems like we can assume math? Or is that a naive assumption?

@lukeapage
Copy link
Member

yes it attempts maths on maths objects... e.g. the parser parses 1 + 3 and if in parenthesis then at evaluation time, that is converted into 4.. but at parse time this is parsed as two values.. the parser doesn't know about strict maths and whether two values should be added together.

So, the structure of the two trees is different... Parenthesis -> Value and Parenthesis -> Operation. Either the parser would have to be aware of context and treat it different, or at evaluation time Value would have to say "am I in parenthesis" and convert itself to an operation.

Making the parser aware of parenthesis and maths is probably the better solutuion, but a hell of a lot of effort for some less that is confusing and has never been parsed in the past.

@SomMeri
Copy link
Member Author

SomMeri commented Mar 15, 2013

Thank you for all explanations. I was writing blog post about less, run into this and got confused. I will put it into "quirks" chapter and leave it at that.

@SomMeri
Copy link
Member Author

SomMeri commented Mar 16, 2013

Hm, I tried to play around the idea somewhat more. Altrough I do not know how exactly less.js works, this task seems to be even more complicated.

Less lazy load variables and lazy evaluates expressions. So, if the solution would keep using lazy rules, the variable 1 -2 would have double meaning:

@list-or-expression: 1 -2;

#use {
  as-list: extract(@list-or-expression, 1); // compiles into "as-list: 1;"
  as-number: (@list-or-expression + 1);  // compiles into "as-number: 0;"
}

Ratio vs division works the same way, but they are closer to each other then list and expression:

@ratio-or-expression: 2/1;
#use {
  as-ratio: @ratio-or-expression; // compiles into "as-ratio: 2/1;"
  as-expression: (@ratio-or-expression + 1);  // compiles into "as-expression: 3;"
}

Bleh.

@lukeapage
Copy link
Member

hrmm list-or-expression is pretty strange. That should probably err...?

@SomMeri
Copy link
Member Author

SomMeri commented Mar 16, 2013

Yes, this will currently err as-number: (@list-or-expression + 1); currently. It was meant as "what would it do if 1 -2 would be parsed also as an expression".

@lukeapage
Copy link
Member

I think it is not worth fixing and we should keep it as a syntax error. It looks like it should be valid because in other languages space is not a separator converting it to a list.

Reading the mdn documentation on calc

https://developer.mozilla.org/en-US/docs/CSS/calc

In CSS calc(12px -1px) is invalid.. so given the complications, not supporting it in less either is IMO reasonable

@SomMeri
Copy link
Member Author

SomMeri commented Mar 22, 2013

I will close it then.

@SomMeri SomMeri closed this as completed Mar 22, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants