Skip to content

Commit

Permalink
Fix #1272, leading underscore gets parsed funny
Browse files Browse the repository at this point in the history
  • Loading branch information
evancz committed Dec 18, 2016
1 parent 220df1e commit 69518e3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
8 changes: 1 addition & 7 deletions src/Parse/Helpers.hs
Expand Up @@ -4,7 +4,7 @@ module Parse.Helpers
( module Parse.Primitives
, SParser
, qualifiedVar, qualifiedCapVar
, equals, rightArrow, hasType, comma, pipe, cons, dot, minus, underscore, lambda
, equals, rightArrow, hasType, comma, pipe, cons, dot, minus, lambda
, leftParen, rightParen, leftSquare, rightSquare, leftCurly, rightCurly
, addLocation, inContext
, spaces, checkSpace, checkAligned, checkFreshLine
Expand Down Expand Up @@ -119,12 +119,6 @@ minus =
symbol "-"


{-# INLINE underscore #-}
underscore :: Parser ()
underscore =
symbol "_"


{-# INLINE lambda #-}
lambda :: Parser ()
lambda =
Expand Down
19 changes: 18 additions & 1 deletion src/Parse/Primitives.hs
Expand Up @@ -5,7 +5,7 @@ module Parse.Primitives
, run, runAt
, try, deadend, hint, endOfFile
, oneOf
, symbol, keyword, keywords
, symbol, underscore, keyword, keywords
, lowVar, capVar, infixOp
, getPosition, getCol
, getContext, pushContext, popContext
Expand Down Expand Up @@ -318,6 +318,23 @@ moveCursor array offset length row col =
moveCursor array (offset + 2) (length - 2) row (col + 1)


underscore :: Parser ()
underscore =
Parser $ \(State array offset length indent row col ctx) cok cerr _ eerr ->
if length == 0 || Text.unsafeIndex array offset /= 0x005F {- _ -} then
eerr noError

else if length >= 2 && isInnerVarChar (peekChar array (offset + 1)) then
cerr (ParseError row (col + 1) (BadUnderscore (peekChar array (offset + 1))))

else
let
!newState =
State array (offset + 1) (length - 1) indent row (col + 1) ctx
in
cok () newState noError



-- KEYWORDS

Expand Down
10 changes: 10 additions & 0 deletions src/Reporting/Error/Syntax.hs
Expand Up @@ -75,6 +75,7 @@ data Problem
| BadNumberHex
| BadNumberZero
| BadShader Text
| BadUnderscore Char
| BadOp BadOp ContextStack
| Theories ContextStack [Theory]

Expand Down Expand Up @@ -528,6 +529,15 @@ problemToReport subRegion problem =
"I ran into a problem while parsing this GLSL block."
(reflowParagraph msg)

BadUnderscore char ->
parseReport
("An underscore cannot be followed by `" <> Text.singleton char <> "`.")
( reflowParagraph $
"Variable names cannot start with an underscore in Elm. You can\
\ use an underscore as a \"wildcard\" that matches anything\
\ though. So maybe you forgot a space after the underscore?"
)

BadOp op stack ->
case op of
HasType ->
Expand Down

0 comments on commit 69518e3

Please sign in to comment.