on master help sign in
Move the wildcard parser into Parse.Pattern
commit1709f1ca06124d61d707ba79341b027c528c4f41 authorEvan Czaplicki <evancz@users.noreply.github.com> dateThu 9 May 2019, 11:27 (7 years ago) parente43ce3d
2 files changed+42−35
compiler/src/Parse/Pattern.hs+25−3 compiler/src/Parse/Symbol.hs+17−32
compiler/src/Parse/Pattern.hs+25−3
@@ -1,5 +1,5 @@
11{-# OPTIONS_GHC -Wall -fno-warn-unused-do-bind #-}
2−{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE BangPatterns, UnboxedTuples, OverloadedStrings #-}
33module Parse.Pattern
44 ( term
55 , expression
@@ -10,12 +10,12 @@ module Parse.Pattern
1010import qualified Data.List as List
1111import qualified Data.Name as Name
1212import qualified Data.Utf8 as Utf8
13+import Foreign.Ptr (plusPtr)
1314
1415import qualified AST.Source as Src
1516import qualified Parse.Keyword as Keyword
1617import qualified Parse.Number as Number
1718import qualified Parse.Space as Space
18−import qualified Parse.Symbol as Symbol
1919import qualified Parse.Utf8 as Utf8
2020import qualified Parse.Variable as Var
2121import qualified Parse.Primitives as P
@@ -43,7 +43,7 @@ termHelp :: A.Position -> Parser E.Pattern Src.Pattern
4343termHelp start =
4444 oneOf E.PStart
4545 [
46− do Symbol.wildcard
46+ do wildcard
4747 addEnd start Src.PAnything
4848 ,
4949 do name <- Var.lower E.PStart
@@ -79,6 +79,28 @@ termHelp start =
7979
8080
8181
82+-- WILDCARD
83+
84+
85+wildcard :: Parser E.Pattern ()
86+wildcard =
87+ P.Parser $ \(P.State pos end indent row col) cok _ cerr eerr ->
88+ if pos == end || P.unsafeIndex pos /= 0x5F {- _ -} then
89+ eerr row col E.PStart
90+ else
91+ let
92+ !newPos = plusPtr pos 1
93+ !newCol = col + 1
94+ in
95+ if Var.getInnerWidth newPos end > 0 then
96+ let (# _, badCol #) = Var.chompInnerChars newPos end newCol in
97+ cerr row col (E.PWildcardNotVar (fromIntegral (badCol - col)))
98+ else
99+ let !newState = P.State newPos end indent row newCol in
100+ cok () newState
101+
102+
103+
82104-- RECORDS
83105
84106
compiler/src/Parse/Symbol.hs+17−32
@@ -1,8 +1,9 @@
11{-# OPTIONS_GHC -Wall #-}
2−{-# LANGUAGE BangPatterns, UnboxedTuples, OverloadedStrings #-}
2+{-# LANGUAGE BangPatterns, OverloadedStrings #-}
33module Parse.Symbol
4− ( wildcard
5− , operator
4+ ( operator
5+ , BadOperator(..)
6+ , binopCharSet
67 )
78 where
89
@@ -15,38 +16,22 @@ import Foreign.Ptr (Ptr, plusPtr, minusPtr)
1516import GHC.Word (Word8)
1617
1718import Parse.Primitives (Parser, Row, Col)
18−import qualified Parse.Variable as Var
1919import qualified Parse.Primitives as P
20−import qualified Reporting.Error.Syntax as E
2120
2221
2322
24−-- UNDERSCORE
25−
26−
27−wildcard :: Parser E.Pattern ()
28−wildcard =
29− P.Parser $ \(P.State pos end indent row col) cok _ cerr eerr ->
30− if pos == end || P.unsafeIndex pos /= 0x5F {- _ -} then
31− eerr row col E.PStart
32− else
33− let
34− !newPos = plusPtr pos 1
35− !newCol = col + 1
36− in
37− if Var.getInnerWidth newPos end > 0 then
38− let (# _, badCol #) = Var.chompInnerChars newPos end newCol in
39− cerr row col (E.PWildcardNotVar (fromIntegral (badCol - col)))
40− else
41− let !newState = P.State newPos end indent row newCol in
42− cok () newState
43−
23+-- OPERATOR
4424
4525
46−-- OPERATOR
26+data BadOperator
27+ = BadDot
28+ | BadPipe
29+ | BadArrow
30+ | BadEquals
31+ | BadHasType
4732
4833
49−operator :: (Row -> Col -> x) -> (E.Operator -> Row -> Col -> x) -> Parser x Name.Name
34+operator :: (Row -> Col -> x) -> (BadOperator -> Row -> Col -> x) -> Parser x Name.Name
5035operator toExpectation toError =
5136 P.Parser $ \(P.State pos end indent row col) cok _ cerr eerr ->
5237 let !newPos = chompOps pos end in
@@ -55,11 +40,11 @@ operator toExpectation toError =
5540
5641 else
5742 case Name.fromPtr pos newPos of
58− "." -> cerr row col (toError E.OpDot)
59− "|" -> cerr row col (toError E.OpPipe)
60− "->" -> cerr row col (toError E.OpArrow)
61− "=" -> cerr row col (toError E.OpEquals)
62− ":" -> cerr row col (toError E.OpHasType)
43+ "." -> cerr row col (toError BadDot)
44+ "|" -> cerr row col (toError BadPipe)
45+ "->" -> cerr row col (toError BadArrow)
46+ "=" -> cerr row col (toError BadEquals)
47+ ":" -> cerr row col (toError BadHasType)
6348 op ->
6449 let
6550 !newCol = col + fromIntegral (minusPtr newPos pos)