Problem with natural numbers
See original GitHub issueI’m at a bit of a loss as to why this is happening so I thought I’d ask here. Following is a small reproduction of what I’m trying to achieve.
I have the following grammar:
{
"$schema": "https://raw.githubusercontent.com/martinring/tmlanguage/master/tmlanguage.json",
"name": "Unison",
"patterns": [{
"match": "([^\\+\\-\\w\\d]|^)(\\d+)",
"captures": {
"2": {
"name": "constant.numeric.integer.unsigned.unison"
}
}
},{
"match": "([^\\w\\d]|^)([+-]\\d+)",
"captures": {
"2": {
"name": "constant.numeric.integer.signed.unison"
}
}
}],
"scopeName": "source.u"
}
if I build a Grammar
from this json and try to tokenize +42
, I get as tokens
[ { startIndex: 0,
endIndex: 3,
scopes: [ 'source.u', 'constant.numeric.integer.signed.unison' ] } ]
which is expected.
However when I tokenize 42
, I get:
[ { startIndex: 0, endIndex: 3, scopes: [ 'source.u' ] } ]
without the unsigned scope.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Natural numbers - practice problems - HackMath.net
Natural numbers - word problems. Solved word problems, tests, exercises, and preparation for exams. Math questions with answers. Problems count 1418.
Read more >Natural Number Word Problems - Superprof
This lesson contains word problems of natural numbers with their solutions and answers.
Read more >Natural number - Art of Problem Solving
A natural number is any positive integer: $\text{1, 2, 3, 4, 5, 6, . The set of natural numbers, denoted $\mathbb{N}$ , is...
Read more >Natural Numbers - Concepts, Properties, Number Line ... - Byju's
Every natural number is a whole number. The statement is true because natural numbers are the positive integers that start from 1 and...
Read more >Natural Number Questions and Answers - Study.com
Access the answers to hundreds of Natural number questions that are explained ... Test your understanding with practice problems and step-by-step solutions.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Its because your first part of the rule says that you cannot match a digit, and the 4 is a digit. I bet
42
‘(space)42’ works.I think you meant the first part to be a look behind. I recommend staying away from look behinds. Always try matching going forward, as looking behind can set you up for things that are not the way they seem. For instance, looking behind for a quote, was the quote escaped? You’d have to look behind further, but wait, was the escape escaped? When you first start out writing a grammar you will want to use look behinds because you haven’t handled much yet, but you should try to refrain from that, save the failures as TODO items . Once everything is scoped, theres almost no reason to look behind.
For clarification, I am still not quite right.
\b
always gives me trouble.\b
works like a look behind (hence it should be refrained from being used) as(?<=\w)(?=\W)|(?<=\W)(?=\w)
. So, ‘(space)+’ doesn’t count as a \b between the space and the ‘+’, so it fails. It would instead require:"match": "\\b(\\d+)"
, and"match": "(?<=\W|^)([+-]\\d+)"
,This just demonstrates why look behinds should be avoided. Instead, all text should be matched by rules looking in the forward direction only. Only use look-behinds when you know exactly what could have been matched previously.