on master help sign in
Fix #1272, leading underscore gets parsed funny
69518e3Evan Czaplicki
src/Parse/Helpers.hs+1−7
@@ -4,7 +4,7 @@ module Parse.Helpers
4 ( module Parse.Primitives
4 ( module Parse.Primitives
5 , SParser
5 , SParser
6 , qualifiedVar, qualifiedCapVar
6 , qualifiedVar, qualifiedCapVar
7− , equals, rightArrow, hasType, comma, pipe, cons, dot, minus, underscore, lambda
7+ , equals, rightArrow, hasType, comma, pipe, cons, dot, minus, lambda
8 , leftParen, rightParen, leftSquare, rightSquare, leftCurly, rightCurly
8 , leftParen, rightParen, leftSquare, rightSquare, leftCurly, rightCurly
9 , addLocation, inContext
9 , addLocation, inContext
10 , spaces, checkSpace, checkAligned, checkFreshLine
10 , spaces, checkSpace, checkAligned, checkFreshLine
@@ -119,12 +119,6 @@ minus =
119 symbol "-"
119 symbol "-"
120
120
121
121
122−{-# INLINE underscore #-}
123−underscore :: Parser ()
124−underscore =
125− symbol "_"
126−
127−
128{-# INLINE lambda #-}
122{-# INLINE lambda #-}
129lambda :: Parser ()
123lambda :: Parser ()
130lambda =
124lambda =
src/Parse/Primitives.hs+18−1
@@ -5,7 +5,7 @@ module Parse.Primitives
5 , run, runAt
5 , run, runAt
6 , try, deadend, hint, endOfFile
6 , try, deadend, hint, endOfFile
7 , oneOf
7 , oneOf
8− , symbol, keyword, keywords
8+ , symbol, underscore, keyword, keywords
9 , lowVar, capVar, infixOp
9 , lowVar, capVar, infixOp
10 , getPosition, getCol
10 , getPosition, getCol
11 , getContext, pushContext, popContext
11 , getContext, pushContext, popContext
@@ -318,6 +318,23 @@ moveCursor array offset length row col =
318 moveCursor array (offset + 2) (length - 2) row (col + 1)
318 moveCursor array (offset + 2) (length - 2) row (col + 1)
319
319
320
320
321+underscore :: Parser ()
322+underscore =
323+ Parser $ \(State array offset length indent row col ctx) cok cerr _ eerr ->
324+ if length == 0 || Text.unsafeIndex array offset /= 0x005F {- _ -} then
325+ eerr noError
326+
327+ else if length >= 2 && isInnerVarChar (peekChar array (offset + 1)) then
328+ cerr (ParseError row (col + 1) (BadUnderscore (peekChar array (offset + 1))))
329+
330+ else
331+ let
332+ !newState =
333+ State array (offset + 1) (length - 1) indent row (col + 1) ctx
334+ in
335+ cok () newState noError
336+
337+
321
338
322-- KEYWORDS
339-- KEYWORDS
323
340
src/Reporting/Error/Syntax.hs+10
@@ -75,6 +75,7 @@ data Problem
75 | BadNumberHex
75 | BadNumberHex
76 | BadNumberZero
76 | BadNumberZero
77 | BadShader Text
77 | BadShader Text
78+ | BadUnderscore Char
78 | BadOp BadOp ContextStack
79 | BadOp BadOp ContextStack
79 | Theories ContextStack [Theory]
80 | Theories ContextStack [Theory]
80
81
@@ -528,6 +529,15 @@ problemToReport subRegion problem =
528 "I ran into a problem while parsing this GLSL block."
529 "I ran into a problem while parsing this GLSL block."
529 (reflowParagraph msg)
530 (reflowParagraph msg)
530
531
532+ BadUnderscore char ->
533+ parseReport
534+ ("An underscore cannot be followed by `" <> Text.singleton char <> "`.")
535+ ( reflowParagraph $
536+ "Variable names cannot start with an underscore in Elm. You can\
537+ \ use an underscore as a \"wildcard\" that matches anything\
538+ \ though. So maybe you forgot a space after the underscore?"
539+ )
540+
531 BadOp op stack ->
541 BadOp op stack ->
532 case op of
542 case op of
533 HasType ->
543 HasType ->