Simplified the Rain parser by reintroducing a RangeLiteral item that will get replaced by an early pass
This commit is contained in:
parent
bb6937433f
commit
41cca68e4e
|
@ -227,6 +227,9 @@ data LiteralRepr =
|
||||||
| ByteLiteral Meta String
|
| ByteLiteral Meta String
|
||||||
-- | This can be for arrays and for lists
|
-- | This can be for arrays and for lists
|
||||||
| ArrayListLiteral Meta (Structured Expression)
|
| ArrayListLiteral Meta (Structured Expression)
|
||||||
|
-- | This is transformed out very early on. The first item is the start of the
|
||||||
|
-- range, the second is the end of the range (both inclusive)
|
||||||
|
| RangeLiteral Meta Expression Expression
|
||||||
| RecordLiteral Meta [Expression]
|
| RecordLiteral Meta [Expression]
|
||||||
deriving (Show, Eq, Typeable, Data)
|
deriving (Show, Eq, Typeable, Data)
|
||||||
|
|
||||||
|
|
|
@ -35,7 +35,6 @@ import qualified LexRain as L
|
||||||
import Metadata
|
import Metadata
|
||||||
import ParseUtils
|
import ParseUtils
|
||||||
import Pass
|
import Pass
|
||||||
import Types
|
|
||||||
import Utils
|
import Utils
|
||||||
|
|
||||||
type RainState = CompState
|
type RainState = CompState
|
||||||
|
@ -266,20 +265,16 @@ range = try $ do m <- sLeftQ
|
||||||
sDots
|
sDots
|
||||||
end <- literal
|
end <- literal
|
||||||
sRightQ
|
sRightQ
|
||||||
(t, rep) <- case optTy of
|
(t, be) <- case optTy of
|
||||||
Just (t, mc) ->
|
Just (t, mc) ->
|
||||||
let begin' = A.Conversion mc A.DefaultConversion t begin
|
let begin' = A.Conversion mc A.DefaultConversion t begin
|
||||||
end' = A.Conversion mc A.DefaultConversion t end
|
end' = A.Conversion mc A.DefaultConversion t end
|
||||||
in return (t, A.For m begin' (subOne $ addExprs begin' end') (makeConstant m 1))
|
in return (A.List t, (begin', end'))
|
||||||
Nothing -> do u <- getUniqueIdentifer
|
Nothing -> do u <- getUniqueIdentifer
|
||||||
let t = A.List $ A.UnknownVarType (A.TypeRequirements
|
let t = A.List $ A.UnknownVarType (A.TypeRequirements
|
||||||
False) (Right (m,u))
|
False) (Right (m,u))
|
||||||
return (t, A.For m begin
|
return (t, (begin, end))
|
||||||
(subOne $ addExprs begin end) (makeConstant m 1))
|
return $ A.Literal m t $ A.RangeLiteral m (fst be) (snd be)
|
||||||
repSpec@(A.Specification _ repN _) <- defineNonce m "range_rep" (A.Rep m rep) A.ValAbbrev
|
|
||||||
return $ A.Literal m t $ A.ArrayListLiteral m $
|
|
||||||
A.Spec m repSpec $ A.Only m $
|
|
||||||
A.ExprVariable m $ A.Variable m repN
|
|
||||||
|
|
||||||
expression :: RainParser A.Expression
|
expression :: RainParser A.Expression
|
||||||
expression
|
expression
|
||||||
|
|
|
@ -82,7 +82,8 @@ intLiteralPattern :: Integer -> Pattern
|
||||||
intLiteralPattern = pat . intLiteral
|
intLiteralPattern = pat . intLiteral
|
||||||
|
|
||||||
makeListLiteralPattern :: [Pattern] -> Pattern
|
makeListLiteralPattern :: [Pattern] -> Pattern
|
||||||
makeListLiteralPattern items = mLiteral (A.List inferExpType) (mListLiteral items)
|
makeListLiteralPattern items = mLiteral (A.List inferExpType) (mArrayListLiteral $
|
||||||
|
mSeveralE items)
|
||||||
|
|
||||||
|
|
||||||
-- | Runs a parse test, given a tuple of: (source text, parser function, assertion)
|
-- | Runs a parse test, given a tuple of: (source text, parser function, assertion)
|
||||||
|
@ -286,13 +287,13 @@ testLiteral =
|
||||||
|
|
||||||
-- Lists:
|
-- Lists:
|
||||||
,pass ("[0]", RP.literal, assertPatternMatch "testLiteral 400" $ pat $
|
,pass ("[0]", RP.literal, assertPatternMatch "testLiteral 400" $ pat $
|
||||||
makeListLiteralPattern [intLiteralPattern 0])
|
makeListLiteralPattern [mOnlyE $ intLiteralPattern 0])
|
||||||
,pass ("[]", RP.literal, assertPatternMatch "testLiteral 401" $ pat $
|
,pass ("[]", RP.literal, assertPatternMatch "testLiteral 401" $ pat $
|
||||||
makeListLiteralPattern [])
|
makeListLiteralPattern [])
|
||||||
,pass ("[0,1,2]", RP.literal, assertPatternMatch "testLiteral 402" $ pat $
|
,pass ("[0,1,2]", RP.literal, assertPatternMatch "testLiteral 402" $ pat $
|
||||||
makeListLiteralPattern $ map intLiteralPattern [0,1,2])
|
makeListLiteralPattern $ map (mOnlyE . intLiteralPattern) [0,1,2])
|
||||||
,pass ("['0']", RP.literal, assertPatternMatch "testLiteral 403" $ pat $
|
,pass ("['0']", RP.literal, assertPatternMatch "testLiteral 403" $ pat $
|
||||||
makeListLiteralPattern [makeLiteralCharPattern '0'])
|
makeListLiteralPattern [mOnlyE $ makeLiteralCharPattern '0'])
|
||||||
|
|
||||||
,fail ("[", RP.literal)
|
,fail ("[", RP.literal)
|
||||||
,fail ("]", RP.literal)
|
,fail ("]", RP.literal)
|
||||||
|
@ -306,11 +307,11 @@ testRange :: [ParseTest A.Expression]
|
||||||
testRange =
|
testRange =
|
||||||
[
|
[
|
||||||
pass("[0..1]", RP.expression, assertPatternMatch "testRange 0" $ pat $
|
pass("[0..1]", RP.expression, assertPatternMatch "testRange 0" $ pat $
|
||||||
A.ExprConstr m $ A.RangeConstr m (A.List inferExpType) (intLiteral 0) (intLiteral 1))
|
A.Literal m (A.List inferExpType) $ A.RangeLiteral m (intLiteral 0) (intLiteral 1))
|
||||||
,pass("[0..10000]", RP.expression, assertPatternMatch "testRange 1" $ pat $
|
,pass("[0..10000]", RP.expression, assertPatternMatch "testRange 1" $ pat $
|
||||||
A.ExprConstr m $ A.RangeConstr m (A.List inferExpType) (intLiteral 0) (intLiteral 10000))
|
A.Literal m (A.List inferExpType) $ A.RangeLiteral m (intLiteral 0) (intLiteral 10000))
|
||||||
,pass("[-3..-1]", RP.expression, assertPatternMatch "testRange 2" $ pat $
|
,pass("[-3..-1]", RP.expression, assertPatternMatch "testRange 2" $ pat $
|
||||||
A.ExprConstr m $ A.RangeConstr m (A.List inferExpType) (intLiteral $ -3) (intLiteral $ -1))
|
A.Literal m (A.List inferExpType) $ A.RangeLiteral m (intLiteral $ -3) (intLiteral $ -1))
|
||||||
,pass("[sint16: 0..1]", RP.expression, rangePattern 4 (A.List A.Int16)
|
,pass("[sint16: 0..1]", RP.expression, rangePattern 4 (A.List A.Int16)
|
||||||
(buildExprPattern $ Cast A.Int16 (Lit $ intLiteral 0))
|
(buildExprPattern $ Cast A.Int16 (Lit $ intLiteral 0))
|
||||||
(buildExprPattern $ Cast A.Int16 (Lit $ intLiteral 1)))
|
(buildExprPattern $ Cast A.Int16 (Lit $ intLiteral 1)))
|
||||||
|
@ -321,7 +322,8 @@ testRange =
|
||||||
where
|
where
|
||||||
rangePattern :: Int -> A.Type -> Pattern -> Pattern -> (A.Expression -> Assertion)
|
rangePattern :: Int -> A.Type -> Pattern -> Pattern -> (A.Expression -> Assertion)
|
||||||
rangePattern n t start end = assertPatternMatch ("testRange " ++ show n) $
|
rangePattern n t start end = assertPatternMatch ("testRange " ++ show n) $
|
||||||
pat $ mExprConstr $ mRangeConstr t start end
|
pat $ mLiteral t $ mRangeLiteral start end
|
||||||
|
|
||||||
|
|
||||||
--Helper function for ifs:
|
--Helper function for ifs:
|
||||||
makeIf :: [(A.Expression,A.Process)] -> A.Process
|
makeIf :: [(A.Expression,A.Process)] -> A.Process
|
||||||
|
|
Loading…
Reference in New Issue
Block a user