on master help sign in
Fix #1272, leading underscore gets parsed funny
commit69518e35e59f985abcea3b8e6325269c31bf97c4 authorEvan Czaplicki <evancz@users.noreply.github.com> dateSat 17 Dec 2016, 20:36 (10 years ago) parent220df1e
3 files changed+29−8
src/Parse/Helpers.hs+1−7 src/Parse/Primitives.hs+18−1 src/Reporting/Error/Syntax.hs+10
src/Parse/Helpers.hs+1−7
@@ -4,7 +4,7 @@ module Parse.Helpers
44 ( module Parse.Primitives
55 , SParser
66 , qualifiedVar, qualifiedCapVar
7− , equals, rightArrow, hasType, comma, pipe, cons, dot, minus, underscore, lambda
7+ , equals, rightArrow, hasType, comma, pipe, cons, dot, minus, lambda
88 , leftParen, rightParen, leftSquare, rightSquare, leftCurly, rightCurly
99 , addLocation, inContext
1010 , spaces, checkSpace, checkAligned, checkFreshLine
@@ -119,12 +119,6 @@ minus =
119119 symbol "-"
120120
121121
122−{-# INLINE underscore #-}
123−underscore :: Parser ()
124−underscore =
125− symbol "_"
126−
127−
128122{-# INLINE lambda #-}
129123lambda :: Parser ()
130124lambda =
src/Parse/Primitives.hs+18−1
@@ -5,7 +5,7 @@ module Parse.Primitives
55 , run, runAt
66 , try, deadend, hint, endOfFile
77 , oneOf
8− , symbol, keyword, keywords
8+ , symbol, underscore, keyword, keywords
99 , lowVar, capVar, infixOp
1010 , getPosition, getCol
1111 , getContext, pushContext, popContext
@@ -318,6 +318,23 @@ moveCursor array offset length row col =
318318 moveCursor array (offset + 2) (length - 2) row (col + 1)
319319
320320
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+
321338
322339-- KEYWORDS
323340
src/Reporting/Error/Syntax.hs+10
@@ -75,6 +75,7 @@ data Problem
7575 | BadNumberHex
7676 | BadNumberZero
7777 | BadShader Text
78+ | BadUnderscore Char
7879 | BadOp BadOp ContextStack
7980 | Theories ContextStack [Theory]
8081
@@ -528,6 +529,15 @@ problemToReport subRegion problem =
528529 "I ran into a problem while parsing this GLSL block."
529530 (reflowParagraph msg)
530531
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+
531541 BadOp op stack ->
532542 case op of
533543 HasType ->