2018-04-12 23:22:55 +02:00
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
2014-08-03 14:17:43 +02:00
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2018-04-06 08:16:55 +02:00
|
|
|
{-# LANGUAGE RankNTypes #-}
|
|
|
|
|
|
|
|
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
|
2018-04-12 23:22:55 +02:00
|
|
|
{-# OPTIONS_GHC -Wno-missing-signatures -Wno-orphans #-}
|
2018-04-06 08:16:55 +02:00
|
|
|
|
2014-08-03 14:17:43 +02:00
|
|
|
module ParserTests (tests) where
|
|
|
|
|
2018-04-10 20:58:08 +02:00
|
|
|
import Data.Fix
|
2018-04-17 21:46:41 +02:00
|
|
|
import Data.List.NonEmpty (NonEmpty(..))
|
2018-04-10 20:58:08 +02:00
|
|
|
import Data.Semigroup
|
2018-04-12 23:22:55 +02:00
|
|
|
import Data.String.Interpolate.IsString
|
|
|
|
import Data.Text (Text, unpack, pack)
|
2018-04-10 20:58:08 +02:00
|
|
|
import Nix.Atoms
|
|
|
|
import Nix.Expr
|
|
|
|
import Nix.Parser
|
2018-04-12 23:22:55 +02:00
|
|
|
import Nix.Pretty
|
2018-04-10 20:58:08 +02:00
|
|
|
import Test.Tasty
|
|
|
|
import Test.Tasty.HUnit
|
|
|
|
import Test.Tasty.TH
|
2018-04-12 23:22:55 +02:00
|
|
|
import Text.PrettyPrint.ANSI.Leijen hiding ((<$>), (<>))
|
2014-08-03 14:17:43 +02:00
|
|
|
|
2018-04-10 02:12:16 +02:00
|
|
|
case_constant_int = assertParseText "234" $ mkInt 234
|
2014-08-05 21:23:28 +02:00
|
|
|
|
|
|
|
case_constant_bool = do
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "true" $ mkBool True
|
|
|
|
assertParseText "false" $ mkBool False
|
2014-08-03 14:17:43 +02:00
|
|
|
|
2018-03-06 02:07:35 +01:00
|
|
|
case_constant_bool_respects_attributes = do
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "true-foo" $ mkSym "true-foo"
|
|
|
|
assertParseText "false-bar" $ mkSym "false-bar"
|
2018-03-06 02:07:35 +01:00
|
|
|
|
2014-08-16 00:16:12 +02:00
|
|
|
case_constant_path = do
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "./." $ mkPath False "./."
|
|
|
|
assertParseText "./+-_/cdef/09ad+-" $ mkPath False "./+-_/cdef/09ad+-"
|
|
|
|
assertParseText "/abc" $ mkPath False "/abc"
|
|
|
|
assertParseText "../abc" $ mkPath False "../abc"
|
|
|
|
assertParseText "<abc>" $ mkPath True "abc"
|
|
|
|
assertParseText "<../cdef>" $ mkPath True "../cdef"
|
|
|
|
assertParseText "a//b" $ mkOper2 NUpdate (mkSym "a") (mkSym "b")
|
|
|
|
assertParseText "rec+def/cdef" $ mkPath False "rec+def/cdef"
|
|
|
|
assertParseText "a/b//c/def//<g> < def/d" $ mkOper2 NLt
|
2014-08-20 15:47:41 +02:00
|
|
|
(mkOper2 NUpdate (mkPath False "a/b") $ mkOper2 NUpdate
|
|
|
|
(mkPath False "c/def") (mkPath True "g"))
|
|
|
|
(mkPath False "def/d")
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "a'b/c" $ Fix $ NBinary NApp (mkSym "a'b") (mkPath False "/c")
|
|
|
|
assertParseText "a/b" $ mkPath False "a/b"
|
|
|
|
assertParseText "4/2" $ mkPath False "4/2"
|
2014-08-16 00:16:12 +02:00
|
|
|
assertParseFail "."
|
|
|
|
assertParseFail ".."
|
|
|
|
assertParseFail "/"
|
2014-08-20 15:47:41 +02:00
|
|
|
assertParseFail "a/"
|
2014-08-30 10:10:28 +02:00
|
|
|
assertParseFail "a/def/"
|
2018-04-21 08:17:57 +02:00
|
|
|
assertParseFail "~"
|
|
|
|
assertParseFail "~/"
|
|
|
|
assertParseText "~/a" $ mkPath False "~/a"
|
|
|
|
assertParseText "~/a/b" $ mkPath False "~/a/b"
|
2014-08-30 10:10:28 +02:00
|
|
|
|
|
|
|
case_constant_uri = do
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "a:a" $ mkUri "a:a"
|
|
|
|
assertParseText "http://foo.bar" $ mkUri "http://foo.bar"
|
|
|
|
assertParseText "a+de+.adA+-:%%%ads%5asdk&/" $ mkUri "a+de+.adA+-:%%%ads%5asdk&/"
|
|
|
|
assertParseText "rec+def:c" $ mkUri "rec+def:c"
|
|
|
|
assertParseText "f.foo:bar" $ mkUri "f.foo:bar"
|
2014-08-30 10:10:28 +02:00
|
|
|
assertParseFail "http://foo${\"bar\"}"
|
|
|
|
assertParseFail ":bcdef"
|
|
|
|
assertParseFail "a%20:asda"
|
|
|
|
assertParseFail ".:adasd"
|
|
|
|
assertParseFail "+:acdcd"
|
2014-08-16 00:16:12 +02:00
|
|
|
|
2014-08-13 22:40:35 +02:00
|
|
|
case_simple_set = do
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "{ a = 23; b = 4; }" $ Fix $ NSet
|
antiquotes + improve pretty printer + restructure
This commit improves the pretty printing and adds support for
antiquotes. It also fixes an issue with the parser that caused `[if true
then false else true]` to parse successfully, even though that is not a
valid nix expression.
The pretty printer now produces a lot more readable output and also
supports operator precedences.
The changes to the AST are:
* strings are no longer atomic, because they may contain other
expressions in the form of antiquotes. For strings, the new type
NString is introduced and the constructor NStr is added to NExprF
* the unused NVar constructor of NExprF is removed
* operators are now represented uniformly so that the pretty printer
can lookup information about operators (in particular, associativity
and precedence)
* the NArgs constructor is removed. The first argument of the NAbs
constructor now directly represents the lambda arguments.
* the select and the hasattr operator are moved into NExpr because
they are special (they only accept a selector as second argument, and
select also supports 'or')
The list of operators is now in Types.hs and Parser.hs re-uses that list
to build the parser. This is required because the pretty printer and
parser both need access to operator precedences.
Parser and evaluator also support dynamic attributes and attributes with
dots now. As an example, `let b.a = 3; b.c = { e = {}; }; b.c.e.${"f"} =
4; in b` is parsed and evaluated correctly. As a side effect, NSym
values now don't evaluate to themselves anymore, but instead to the
value retrieved by looking up the variable in the current environment.
Support for evaluating `inherit` bindings was removed because it was
broken before (`{ inherit a; }` would evaluate to a set where the
attribute `a` had the value `NSym a`, not the value of `a`).
The manual Show instances for the AST were replaced by derived
ones, because the manual instances often resulted in output were it was
difficult to determine the missing parentheses.
2014-08-15 22:11:54 +02:00
|
|
|
[ NamedVar (mkSelector "a") $ mkInt 23
|
|
|
|
, NamedVar (mkSelector "b") $ mkInt 4
|
2014-08-13 22:40:35 +02:00
|
|
|
]
|
|
|
|
assertParseFail "{ a = 23 }"
|
|
|
|
|
|
|
|
case_set_inherit = do
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "{ e = 3; inherit a b; }" $ Fix $ NSet
|
antiquotes + improve pretty printer + restructure
This commit improves the pretty printing and adds support for
antiquotes. It also fixes an issue with the parser that caused `[if true
then false else true]` to parse successfully, even though that is not a
valid nix expression.
The pretty printer now produces a lot more readable output and also
supports operator precedences.
The changes to the AST are:
* strings are no longer atomic, because they may contain other
expressions in the form of antiquotes. For strings, the new type
NString is introduced and the constructor NStr is added to NExprF
* the unused NVar constructor of NExprF is removed
* operators are now represented uniformly so that the pretty printer
can lookup information about operators (in particular, associativity
and precedence)
* the NArgs constructor is removed. The first argument of the NAbs
constructor now directly represents the lambda arguments.
* the select and the hasattr operator are moved into NExpr because
they are special (they only accept a selector as second argument, and
select also supports 'or')
The list of operators is now in Types.hs and Parser.hs re-uses that list
to build the parser. This is required because the pretty printer and
parser both need access to operator precedences.
Parser and evaluator also support dynamic attributes and attributes with
dots now. As an example, `let b.a = 3; b.c = { e = {}; }; b.c.e.${"f"} =
4; in b` is parsed and evaluated correctly. As a side effect, NSym
values now don't evaluate to themselves anymore, but instead to the
value retrieved by looking up the variable in the current environment.
Support for evaluating `inherit` bindings was removed because it was
broken before (`{ inherit a; }` would evaluate to a set where the
attribute `a` had the value `NSym a`, not the value of `a`).
The manual Show instances for the AST were replaced by derived
ones, because the manual instances often resulted in output were it was
difficult to determine the missing parentheses.
2014-08-15 22:11:54 +02:00
|
|
|
[ NamedVar (mkSelector "e") $ mkInt 3
|
2018-04-06 08:16:55 +02:00
|
|
|
, Inherit Nothing $ flip StaticKey Nothing <$> ["a", "b"]
|
2014-08-13 22:40:35 +02:00
|
|
|
]
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "{ inherit; }" $ Fix $ NSet [ Inherit Nothing [] ]
|
2014-08-13 22:40:35 +02:00
|
|
|
|
2018-04-10 02:12:16 +02:00
|
|
|
case_set_scoped_inherit = assertParseText "{ inherit (a) b c; e = 4; inherit(a)b c; }" $ Fix $ NSet
|
2018-04-06 08:16:55 +02:00
|
|
|
[ Inherit (Just (mkSym "a")) $ flip StaticKey Nothing <$> ["b", "c"]
|
antiquotes + improve pretty printer + restructure
This commit improves the pretty printing and adds support for
antiquotes. It also fixes an issue with the parser that caused `[if true
then false else true]` to parse successfully, even though that is not a
valid nix expression.
The pretty printer now produces a lot more readable output and also
supports operator precedences.
The changes to the AST are:
* strings are no longer atomic, because they may contain other
expressions in the form of antiquotes. For strings, the new type
NString is introduced and the constructor NStr is added to NExprF
* the unused NVar constructor of NExprF is removed
* operators are now represented uniformly so that the pretty printer
can lookup information about operators (in particular, associativity
and precedence)
* the NArgs constructor is removed. The first argument of the NAbs
constructor now directly represents the lambda arguments.
* the select and the hasattr operator are moved into NExpr because
they are special (they only accept a selector as second argument, and
select also supports 'or')
The list of operators is now in Types.hs and Parser.hs re-uses that list
to build the parser. This is required because the pretty printer and
parser both need access to operator precedences.
Parser and evaluator also support dynamic attributes and attributes with
dots now. As an example, `let b.a = 3; b.c = { e = {}; }; b.c.e.${"f"} =
4; in b` is parsed and evaluated correctly. As a side effect, NSym
values now don't evaluate to themselves anymore, but instead to the
value retrieved by looking up the variable in the current environment.
Support for evaluating `inherit` bindings was removed because it was
broken before (`{ inherit a; }` would evaluate to a set where the
attribute `a` had the value `NSym a`, not the value of `a`).
The manual Show instances for the AST were replaced by derived
ones, because the manual instances often resulted in output were it was
difficult to determine the missing parentheses.
2014-08-15 22:11:54 +02:00
|
|
|
, NamedVar (mkSelector "e") $ mkInt 4
|
2018-04-06 08:16:55 +02:00
|
|
|
, Inherit (Just (mkSym "a")) $ flip StaticKey Nothing <$> ["b", "c"]
|
2014-08-13 22:40:35 +02:00
|
|
|
]
|
|
|
|
|
2018-04-10 02:12:16 +02:00
|
|
|
case_set_rec = assertParseText "rec { a = 3; b = a; }" $ Fix $ NRecSet
|
antiquotes + improve pretty printer + restructure
This commit improves the pretty printing and adds support for
antiquotes. It also fixes an issue with the parser that caused `[if true
then false else true]` to parse successfully, even though that is not a
valid nix expression.
The pretty printer now produces a lot more readable output and also
supports operator precedences.
The changes to the AST are:
* strings are no longer atomic, because they may contain other
expressions in the form of antiquotes. For strings, the new type
NString is introduced and the constructor NStr is added to NExprF
* the unused NVar constructor of NExprF is removed
* operators are now represented uniformly so that the pretty printer
can lookup information about operators (in particular, associativity
and precedence)
* the NArgs constructor is removed. The first argument of the NAbs
constructor now directly represents the lambda arguments.
* the select and the hasattr operator are moved into NExpr because
they are special (they only accept a selector as second argument, and
select also supports 'or')
The list of operators is now in Types.hs and Parser.hs re-uses that list
to build the parser. This is required because the pretty printer and
parser both need access to operator precedences.
Parser and evaluator also support dynamic attributes and attributes with
dots now. As an example, `let b.a = 3; b.c = { e = {}; }; b.c.e.${"f"} =
4; in b` is parsed and evaluated correctly. As a side effect, NSym
values now don't evaluate to themselves anymore, but instead to the
value retrieved by looking up the variable in the current environment.
Support for evaluating `inherit` bindings was removed because it was
broken before (`{ inherit a; }` would evaluate to a set where the
attribute `a` had the value `NSym a`, not the value of `a`).
The manual Show instances for the AST were replaced by derived
ones, because the manual instances often resulted in output were it was
difficult to determine the missing parentheses.
2014-08-15 22:11:54 +02:00
|
|
|
[ NamedVar (mkSelector "a") $ mkInt 3
|
|
|
|
, NamedVar (mkSelector "b") $ mkSym "a"
|
2014-08-13 22:40:35 +02:00
|
|
|
]
|
|
|
|
|
antiquotes + improve pretty printer + restructure
This commit improves the pretty printing and adds support for
antiquotes. It also fixes an issue with the parser that caused `[if true
then false else true]` to parse successfully, even though that is not a
valid nix expression.
The pretty printer now produces a lot more readable output and also
supports operator precedences.
The changes to the AST are:
* strings are no longer atomic, because they may contain other
expressions in the form of antiquotes. For strings, the new type
NString is introduced and the constructor NStr is added to NExprF
* the unused NVar constructor of NExprF is removed
* operators are now represented uniformly so that the pretty printer
can lookup information about operators (in particular, associativity
and precedence)
* the NArgs constructor is removed. The first argument of the NAbs
constructor now directly represents the lambda arguments.
* the select and the hasattr operator are moved into NExpr because
they are special (they only accept a selector as second argument, and
select also supports 'or')
The list of operators is now in Types.hs and Parser.hs re-uses that list
to build the parser. This is required because the pretty printer and
parser both need access to operator precedences.
Parser and evaluator also support dynamic attributes and attributes with
dots now. As an example, `let b.a = 3; b.c = { e = {}; }; b.c.e.${"f"} =
4; in b` is parsed and evaluated correctly. As a side effect, NSym
values now don't evaluate to themselves anymore, but instead to the
value retrieved by looking up the variable in the current environment.
Support for evaluating `inherit` bindings was removed because it was
broken before (`{ inherit a; }` would evaluate to a set where the
attribute `a` had the value `NSym a`, not the value of `a`).
The manual Show instances for the AST were replaced by derived
ones, because the manual instances often resulted in output were it was
difficult to determine the missing parentheses.
2014-08-15 22:11:54 +02:00
|
|
|
case_set_complex_keynames = do
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "{ \"\" = null; }" $ Fix $ NSet
|
2018-04-17 21:46:41 +02:00
|
|
|
[ NamedVar (DynamicKey (Plain "") :| []) mkNull ]
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "{ a.b = 3; a.c = 4; }" $ Fix $ NSet
|
2018-04-17 21:46:41 +02:00
|
|
|
[ NamedVar (StaticKey "a" Nothing :| [StaticKey "b" Nothing]) $ mkInt 3
|
|
|
|
, NamedVar (StaticKey "a" Nothing :| [StaticKey "c" Nothing]) $ mkInt 4
|
antiquotes + improve pretty printer + restructure
This commit improves the pretty printing and adds support for
antiquotes. It also fixes an issue with the parser that caused `[if true
then false else true]` to parse successfully, even though that is not a
valid nix expression.
The pretty printer now produces a lot more readable output and also
supports operator precedences.
The changes to the AST are:
* strings are no longer atomic, because they may contain other
expressions in the form of antiquotes. For strings, the new type
NString is introduced and the constructor NStr is added to NExprF
* the unused NVar constructor of NExprF is removed
* operators are now represented uniformly so that the pretty printer
can lookup information about operators (in particular, associativity
and precedence)
* the NArgs constructor is removed. The first argument of the NAbs
constructor now directly represents the lambda arguments.
* the select and the hasattr operator are moved into NExpr because
they are special (they only accept a selector as second argument, and
select also supports 'or')
The list of operators is now in Types.hs and Parser.hs re-uses that list
to build the parser. This is required because the pretty printer and
parser both need access to operator precedences.
Parser and evaluator also support dynamic attributes and attributes with
dots now. As an example, `let b.a = 3; b.c = { e = {}; }; b.c.e.${"f"} =
4; in b` is parsed and evaluated correctly. As a side effect, NSym
values now don't evaluate to themselves anymore, but instead to the
value retrieved by looking up the variable in the current environment.
Support for evaluating `inherit` bindings was removed because it was
broken before (`{ inherit a; }` would evaluate to a set where the
attribute `a` had the value `NSym a`, not the value of `a`).
The manual Show instances for the AST were replaced by derived
ones, because the manual instances often resulted in output were it was
difficult to determine the missing parentheses.
2014-08-15 22:11:54 +02:00
|
|
|
]
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "{ ${let a = \"b\"; in a} = 4; }" $ Fix $ NSet
|
2018-04-17 21:46:41 +02:00
|
|
|
[ NamedVar (DynamicKey (Antiquoted letExpr) :| []) $ mkInt 4 ]
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "{ \"a${let a = \"b\"; in a}c\".e = 4; }" $ Fix $ NSet
|
2018-04-17 21:46:41 +02:00
|
|
|
[ NamedVar (DynamicKey (Plain str) :| [StaticKey "e" Nothing]) $ mkInt 4 ]
|
antiquotes + improve pretty printer + restructure
This commit improves the pretty printing and adds support for
antiquotes. It also fixes an issue with the parser that caused `[if true
then false else true]` to parse successfully, even though that is not a
valid nix expression.
The pretty printer now produces a lot more readable output and also
supports operator precedences.
The changes to the AST are:
* strings are no longer atomic, because they may contain other
expressions in the form of antiquotes. For strings, the new type
NString is introduced and the constructor NStr is added to NExprF
* the unused NVar constructor of NExprF is removed
* operators are now represented uniformly so that the pretty printer
can lookup information about operators (in particular, associativity
and precedence)
* the NArgs constructor is removed. The first argument of the NAbs
constructor now directly represents the lambda arguments.
* the select and the hasattr operator are moved into NExpr because
they are special (they only accept a selector as second argument, and
select also supports 'or')
The list of operators is now in Types.hs and Parser.hs re-uses that list
to build the parser. This is required because the pretty printer and
parser both need access to operator precedences.
Parser and evaluator also support dynamic attributes and attributes with
dots now. As an example, `let b.a = 3; b.c = { e = {}; }; b.c.e.${"f"} =
4; in b` is parsed and evaluated correctly. As a side effect, NSym
values now don't evaluate to themselves anymore, but instead to the
value retrieved by looking up the variable in the current environment.
Support for evaluating `inherit` bindings was removed because it was
broken before (`{ inherit a; }` would evaluate to a set where the
attribute `a` had the value `NSym a`, not the value of `a`).
The manual Show instances for the AST were replaced by derived
ones, because the manual instances often resulted in output were it was
difficult to determine the missing parentheses.
2014-08-15 22:11:54 +02:00
|
|
|
where
|
2018-04-19 08:42:13 +02:00
|
|
|
letExpr = Fix $ NLet [NamedVar (mkSelector "a") (mkStr "b")] (mkSym "a")
|
2016-01-24 00:18:41 +01:00
|
|
|
str = DoubleQuoted [Plain "a", Antiquoted letExpr, Plain "c"]
|
antiquotes + improve pretty printer + restructure
This commit improves the pretty printing and adds support for
antiquotes. It also fixes an issue with the parser that caused `[if true
then false else true]` to parse successfully, even though that is not a
valid nix expression.
The pretty printer now produces a lot more readable output and also
supports operator precedences.
The changes to the AST are:
* strings are no longer atomic, because they may contain other
expressions in the form of antiquotes. For strings, the new type
NString is introduced and the constructor NStr is added to NExprF
* the unused NVar constructor of NExprF is removed
* operators are now represented uniformly so that the pretty printer
can lookup information about operators (in particular, associativity
and precedence)
* the NArgs constructor is removed. The first argument of the NAbs
constructor now directly represents the lambda arguments.
* the select and the hasattr operator are moved into NExpr because
they are special (they only accept a selector as second argument, and
select also supports 'or')
The list of operators is now in Types.hs and Parser.hs re-uses that list
to build the parser. This is required because the pretty printer and
parser both need access to operator precedences.
Parser and evaluator also support dynamic attributes and attributes with
dots now. As an example, `let b.a = 3; b.c = { e = {}; }; b.c.e.${"f"} =
4; in b` is parsed and evaluated correctly. As a side effect, NSym
values now don't evaluate to themselves anymore, but instead to the
value retrieved by looking up the variable in the current environment.
Support for evaluating `inherit` bindings was removed because it was
broken before (`{ inherit a; }` would evaluate to a set where the
attribute `a` had the value `NSym a`, not the value of `a`).
The manual Show instances for the AST were replaced by derived
ones, because the manual instances often resulted in output were it was
difficult to determine the missing parentheses.
2014-08-15 22:11:54 +02:00
|
|
|
|
2018-04-10 02:12:16 +02:00
|
|
|
case_set_inherit_direct = assertParseText "{ inherit ({a = 3;}); }" $ Fix $ NSet
|
2016-01-24 00:18:41 +01:00
|
|
|
[ flip Inherit [] $ Just $ Fix $ NSet [NamedVar (mkSelector "a") $ mkInt 3]
|
2014-08-03 14:17:43 +02:00
|
|
|
]
|
|
|
|
|
2014-08-15 23:30:24 +02:00
|
|
|
case_inherit_selector = do
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "{ inherit \"a\"; }" $ Fix $ NSet
|
2016-01-25 01:01:45 +01:00
|
|
|
[Inherit Nothing [DynamicKey (Plain "a")]]
|
2014-08-15 23:30:24 +02:00
|
|
|
assertParseFail "{ inherit a.x; }"
|
|
|
|
|
2018-04-10 02:12:16 +02:00
|
|
|
case_int_list = assertParseText "[1 2 3]" $ Fix $ NList
|
2014-08-05 21:23:28 +02:00
|
|
|
[ mkInt i | i <- [1,2,3] ]
|
2014-08-03 14:17:43 +02:00
|
|
|
|
2018-04-10 02:12:16 +02:00
|
|
|
case_int_null_list = assertParseText "[1 2 3 null 4]" $ Fix (NList (map (Fix . NConstant) [NInt 1, NInt 2, NInt 3, NNull, NInt 4]))
|
2014-08-03 14:17:43 +02:00
|
|
|
|
2014-08-15 23:30:24 +02:00
|
|
|
case_mixed_list = do
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "[{a = 3;}.a (if true then null else false) null false 4 [] c.d or null]" $ Fix $ NList
|
2016-01-24 00:18:41 +01:00
|
|
|
[ Fix (NSelect (Fix (NSet [NamedVar (mkSelector "a") (mkInt 3)])) (mkSelector "a") Nothing)
|
2014-08-15 23:30:24 +02:00
|
|
|
, Fix (NIf (mkBool True) mkNull (mkBool False))
|
|
|
|
, mkNull, mkBool False, mkInt 4, Fix (NList [])
|
|
|
|
, Fix (NSelect (mkSym "c") (mkSelector "d") (Just mkNull))
|
|
|
|
]
|
|
|
|
assertParseFail "[if true then null else null]"
|
|
|
|
assertParseFail "[a ? b]"
|
|
|
|
assertParseFail "[a : a]"
|
|
|
|
assertParseFail "[${\"test\")]"
|
|
|
|
|
2018-04-10 02:12:16 +02:00
|
|
|
case_simple_lambda = assertParseText "a: a" $ Fix $ NAbs (Param "a") (mkSym "a")
|
2014-08-03 14:17:43 +02:00
|
|
|
|
2014-08-18 22:54:20 +02:00
|
|
|
case_lambda_or_uri = do
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "a :b" $ Fix $ NAbs (Param "a") (mkSym "b")
|
|
|
|
assertParseText "a c:def" $ Fix $ NBinary NApp (mkSym "a") (mkUri "c:def")
|
|
|
|
assertParseText "c:def: c" $ Fix $ NBinary NApp (mkUri "c:def:") (mkSym "c")
|
|
|
|
assertParseText "a:{}" $ Fix $ NAbs (Param "a") $ Fix $ NSet []
|
|
|
|
assertParseText "a:[a]" $ Fix $ NAbs (Param "a") $ Fix $ NList [mkSym "a"]
|
2014-08-18 22:54:20 +02:00
|
|
|
assertParseFail "def:"
|
|
|
|
|
2014-08-13 23:30:24 +02:00
|
|
|
case_lambda_pattern = do
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "{b, c ? 1}: b" $
|
2016-01-24 01:11:26 +01:00
|
|
|
Fix $ NAbs (fixed args Nothing) (mkSym "b")
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "{ b ? x: x }: b" $
|
2016-01-24 01:11:26 +01:00
|
|
|
Fix $ NAbs (fixed args2 Nothing) (mkSym "b")
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "a@{b,c ? 1}: b" $
|
2016-01-24 01:11:26 +01:00
|
|
|
Fix $ NAbs (fixed args (Just "a")) (mkSym "b")
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "{b,c?1}@a: c" $
|
2016-01-24 01:11:26 +01:00
|
|
|
Fix $ NAbs (fixed args (Just "a")) (mkSym "c")
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "{b,c?1,...}@a: c" $
|
2016-01-24 01:11:26 +01:00
|
|
|
Fix $ NAbs (variadic vargs (Just "a")) (mkSym "c")
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "{...}: 1" $
|
2016-01-24 01:11:26 +01:00
|
|
|
Fix $ NAbs (variadic mempty Nothing) (mkInt 1)
|
2014-08-13 23:30:24 +02:00
|
|
|
assertParseFail "a@b: a"
|
|
|
|
assertParseFail "{a}@{b}: a"
|
|
|
|
where
|
2018-04-02 05:53:19 +02:00
|
|
|
fixed args = ParamSet args False
|
|
|
|
variadic args = ParamSet args True
|
2018-04-10 20:58:08 +02:00
|
|
|
args = [("b", Nothing), ("c", Just $ mkInt 1)]
|
|
|
|
vargs = [("b", Nothing), ("c", Just $ mkInt 1)]
|
|
|
|
args2 = [("b", Just lam)]
|
2016-01-24 00:18:41 +01:00
|
|
|
lam = Fix $ NAbs (Param "x") (mkSym "x")
|
2014-08-13 23:30:24 +02:00
|
|
|
|
2018-04-10 02:12:16 +02:00
|
|
|
case_lambda_app_int = assertParseText "(a: a) 3" $ Fix (NBinary NApp lam int) where
|
2014-08-05 21:23:28 +02:00
|
|
|
int = mkInt 3
|
2016-01-24 00:18:41 +01:00
|
|
|
lam = Fix (NAbs (Param "a") asym)
|
2014-08-05 21:23:28 +02:00
|
|
|
asym = mkSym "a"
|
2014-08-03 14:17:43 +02:00
|
|
|
|
2014-08-13 22:40:35 +02:00
|
|
|
case_simple_let = do
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "let a = 4; in a" $ Fix (NLet binds $ mkSym "a")
|
2014-08-13 22:40:35 +02:00
|
|
|
assertParseFail "let a = 4 in a"
|
|
|
|
where
|
2018-04-19 08:42:13 +02:00
|
|
|
binds = [NamedVar (mkSelector "a") $ mkInt 4]
|
2014-08-03 14:17:43 +02:00
|
|
|
|
2018-04-10 02:12:16 +02:00
|
|
|
case_let_body = assertParseText "let { body = 1; }" letBody
|
2018-02-09 15:29:47 +01:00
|
|
|
where
|
|
|
|
letBody = Fix $ NSelect aset (mkSelector "body") Nothing
|
|
|
|
aset = Fix $ NRecSet [NamedVar (mkSelector "body") (mkInt 1)]
|
|
|
|
|
2014-08-13 22:40:35 +02:00
|
|
|
case_nested_let = do
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "let a = 4; in let b = 5; in a" $ Fix $ NLet
|
2018-04-19 08:42:13 +02:00
|
|
|
[NamedVar (mkSelector "a") (mkInt 4)]
|
|
|
|
(Fix $ NLet [NamedVar (mkSelector "b") (mkInt 5)] $ mkSym "a")
|
2014-08-13 22:40:35 +02:00
|
|
|
assertParseFail "let a = 4; let b = 3; in b"
|
|
|
|
|
|
|
|
case_let_scoped_inherit = do
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "let a = null; inherit (b) c; in c" $ Fix $ NLet
|
2018-04-19 08:42:13 +02:00
|
|
|
[ NamedVar (mkSelector "a") mkNull
|
|
|
|
, Inherit (Just $ mkSym "b") [StaticKey "c" Nothing] ]
|
antiquotes + improve pretty printer + restructure
This commit improves the pretty printing and adds support for
antiquotes. It also fixes an issue with the parser that caused `[if true
then false else true]` to parse successfully, even though that is not a
valid nix expression.
The pretty printer now produces a lot more readable output and also
supports operator precedences.
The changes to the AST are:
* strings are no longer atomic, because they may contain other
expressions in the form of antiquotes. For strings, the new type
NString is introduced and the constructor NStr is added to NExprF
* the unused NVar constructor of NExprF is removed
* operators are now represented uniformly so that the pretty printer
can lookup information about operators (in particular, associativity
and precedence)
* the NArgs constructor is removed. The first argument of the NAbs
constructor now directly represents the lambda arguments.
* the select and the hasattr operator are moved into NExpr because
they are special (they only accept a selector as second argument, and
select also supports 'or')
The list of operators is now in Types.hs and Parser.hs re-uses that list
to build the parser. This is required because the pretty printer and
parser both need access to operator precedences.
Parser and evaluator also support dynamic attributes and attributes with
dots now. As an example, `let b.a = 3; b.c = { e = {}; }; b.c.e.${"f"} =
4; in b` is parsed and evaluated correctly. As a side effect, NSym
values now don't evaluate to themselves anymore, but instead to the
value retrieved by looking up the variable in the current environment.
Support for evaluating `inherit` bindings was removed because it was
broken before (`{ inherit a; }` would evaluate to a set where the
attribute `a` had the value `NSym a`, not the value of `a`).
The manual Show instances for the AST were replaced by derived
ones, because the manual instances often resulted in output were it was
difficult to determine the missing parentheses.
2014-08-15 22:11:54 +02:00
|
|
|
(mkSym "c")
|
2014-08-13 22:40:35 +02:00
|
|
|
assertParseFail "let inherit (b) c in c"
|
|
|
|
|
2014-08-30 10:10:28 +02:00
|
|
|
case_if = do
|
2018-04-19 08:42:13 +02:00
|
|
|
assertParseText "if true then true else false" $
|
|
|
|
Fix $ NIf (mkBool True) (mkBool True) (mkBool False)
|
2014-08-30 10:10:28 +02:00
|
|
|
assertParseFail "if true then false"
|
|
|
|
assertParseFail "else"
|
|
|
|
assertParseFail "if true then false else"
|
|
|
|
assertParseFail "if true then false else false else"
|
|
|
|
assertParseFail "1 + 2 then"
|
|
|
|
|
2014-08-03 16:45:34 +02:00
|
|
|
case_identifier_special_chars = do
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "_a" $ mkSym "_a"
|
|
|
|
assertParseText "a_b" $ mkSym "a_b"
|
|
|
|
assertParseText "a'b" $ mkSym "a'b"
|
|
|
|
assertParseText "a''b" $ mkSym "a''b"
|
|
|
|
assertParseText "a-b" $ mkSym "a-b"
|
|
|
|
assertParseText "a--b" $ mkSym "a--b"
|
|
|
|
assertParseText "a12a" $ mkSym "a12a"
|
2014-08-03 16:45:34 +02:00
|
|
|
assertParseFail ".a"
|
|
|
|
assertParseFail "'a"
|
|
|
|
|
2017-04-17 11:33:57 +02:00
|
|
|
case_identifier_keyword_prefix = do
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "true-name" $ mkSym "true-name"
|
|
|
|
assertParseText "trueName" $ mkSym "trueName"
|
|
|
|
assertParseText "null-name" $ mkSym "null-name"
|
|
|
|
assertParseText "nullName" $ mkSym "nullName"
|
|
|
|
assertParseText "[ null-name ]" $ mkList [ mkSym "null-name" ]
|
2017-04-17 11:33:57 +02:00
|
|
|
|
2018-04-10 02:12:16 +02:00
|
|
|
makeTextParseTest str = assertParseText ("\"" <> str <> "\"") $ mkStr str
|
2014-08-05 21:23:28 +02:00
|
|
|
|
2018-04-10 02:12:16 +02:00
|
|
|
case_simple_string = mapM_ makeTextParseTest ["abcdef", "a", "A", " a a ", ""]
|
2014-08-05 21:23:28 +02:00
|
|
|
|
2018-04-10 02:12:16 +02:00
|
|
|
case_string_dollar = mapM_ makeTextParseTest ["a$b", "a$$b", "$cdef", "gh$i"]
|
2014-08-05 21:23:28 +02:00
|
|
|
|
antiquotes + improve pretty printer + restructure
This commit improves the pretty printing and adds support for
antiquotes. It also fixes an issue with the parser that caused `[if true
then false else true]` to parse successfully, even though that is not a
valid nix expression.
The pretty printer now produces a lot more readable output and also
supports operator precedences.
The changes to the AST are:
* strings are no longer atomic, because they may contain other
expressions in the form of antiquotes. For strings, the new type
NString is introduced and the constructor NStr is added to NExprF
* the unused NVar constructor of NExprF is removed
* operators are now represented uniformly so that the pretty printer
can lookup information about operators (in particular, associativity
and precedence)
* the NArgs constructor is removed. The first argument of the NAbs
constructor now directly represents the lambda arguments.
* the select and the hasattr operator are moved into NExpr because
they are special (they only accept a selector as second argument, and
select also supports 'or')
The list of operators is now in Types.hs and Parser.hs re-uses that list
to build the parser. This is required because the pretty printer and
parser both need access to operator precedences.
Parser and evaluator also support dynamic attributes and attributes with
dots now. As an example, `let b.a = 3; b.c = { e = {}; }; b.c.e.${"f"} =
4; in b` is parsed and evaluated correctly. As a side effect, NSym
values now don't evaluate to themselves anymore, but instead to the
value retrieved by looking up the variable in the current environment.
Support for evaluating `inherit` bindings was removed because it was
broken before (`{ inherit a; }` would evaluate to a set where the
attribute `a` had the value `NSym a`, not the value of `a`).
The manual Show instances for the AST were replaced by derived
ones, because the manual instances often resulted in output were it was
difficult to determine the missing parentheses.
2014-08-15 22:11:54 +02:00
|
|
|
case_string_escape = do
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "\"\\$\\n\\t\\r\\\\\"" $ mkStr "$\n\t\r\\"
|
|
|
|
assertParseText "\" \\\" \\' \"" $ mkStr " \" ' "
|
2014-08-05 21:23:28 +02:00
|
|
|
|
antiquotes + improve pretty printer + restructure
This commit improves the pretty printing and adds support for
antiquotes. It also fixes an issue with the parser that caused `[if true
then false else true]` to parse successfully, even though that is not a
valid nix expression.
The pretty printer now produces a lot more readable output and also
supports operator precedences.
The changes to the AST are:
* strings are no longer atomic, because they may contain other
expressions in the form of antiquotes. For strings, the new type
NString is introduced and the constructor NStr is added to NExprF
* the unused NVar constructor of NExprF is removed
* operators are now represented uniformly so that the pretty printer
can lookup information about operators (in particular, associativity
and precedence)
* the NArgs constructor is removed. The first argument of the NAbs
constructor now directly represents the lambda arguments.
* the select and the hasattr operator are moved into NExpr because
they are special (they only accept a selector as second argument, and
select also supports 'or')
The list of operators is now in Types.hs and Parser.hs re-uses that list
to build the parser. This is required because the pretty printer and
parser both need access to operator precedences.
Parser and evaluator also support dynamic attributes and attributes with
dots now. As an example, `let b.a = 3; b.c = { e = {}; }; b.c.e.${"f"} =
4; in b` is parsed and evaluated correctly. As a side effect, NSym
values now don't evaluate to themselves anymore, but instead to the
value retrieved by looking up the variable in the current environment.
Support for evaluating `inherit` bindings was removed because it was
broken before (`{ inherit a; }` would evaluate to a set where the
attribute `a` had the value `NSym a`, not the value of `a`).
The manual Show instances for the AST were replaced by derived
ones, because the manual instances often resulted in output were it was
difficult to determine the missing parentheses.
2014-08-15 22:11:54 +02:00
|
|
|
case_string_antiquote = do
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "\"abc${ if true then \"def\" else \"abc\" } g\"" $
|
2016-01-24 00:18:41 +01:00
|
|
|
Fix $ NStr $ DoubleQuoted
|
antiquotes + improve pretty printer + restructure
This commit improves the pretty printing and adds support for
antiquotes. It also fixes an issue with the parser that caused `[if true
then false else true]` to parse successfully, even though that is not a
valid nix expression.
The pretty printer now produces a lot more readable output and also
supports operator precedences.
The changes to the AST are:
* strings are no longer atomic, because they may contain other
expressions in the form of antiquotes. For strings, the new type
NString is introduced and the constructor NStr is added to NExprF
* the unused NVar constructor of NExprF is removed
* operators are now represented uniformly so that the pretty printer
can lookup information about operators (in particular, associativity
and precedence)
* the NArgs constructor is removed. The first argument of the NAbs
constructor now directly represents the lambda arguments.
* the select and the hasattr operator are moved into NExpr because
they are special (they only accept a selector as second argument, and
select also supports 'or')
The list of operators is now in Types.hs and Parser.hs re-uses that list
to build the parser. This is required because the pretty printer and
parser both need access to operator precedences.
Parser and evaluator also support dynamic attributes and attributes with
dots now. As an example, `let b.a = 3; b.c = { e = {}; }; b.c.e.${"f"} =
4; in b` is parsed and evaluated correctly. As a side effect, NSym
values now don't evaluate to themselves anymore, but instead to the
value retrieved by looking up the variable in the current environment.
Support for evaluating `inherit` bindings was removed because it was
broken before (`{ inherit a; }` would evaluate to a set where the
attribute `a` had the value `NSym a`, not the value of `a`).
The manual Show instances for the AST were replaced by derived
ones, because the manual instances often resulted in output were it was
difficult to determine the missing parentheses.
2014-08-15 22:11:54 +02:00
|
|
|
[ Plain "abc"
|
2016-01-24 00:18:41 +01:00
|
|
|
, Antiquoted $ Fix $ NIf (mkBool True) (mkStr "def") (mkStr "abc")
|
antiquotes + improve pretty printer + restructure
This commit improves the pretty printing and adds support for
antiquotes. It also fixes an issue with the parser that caused `[if true
then false else true]` to parse successfully, even though that is not a
valid nix expression.
The pretty printer now produces a lot more readable output and also
supports operator precedences.
The changes to the AST are:
* strings are no longer atomic, because they may contain other
expressions in the form of antiquotes. For strings, the new type
NString is introduced and the constructor NStr is added to NExprF
* the unused NVar constructor of NExprF is removed
* operators are now represented uniformly so that the pretty printer
can lookup information about operators (in particular, associativity
and precedence)
* the NArgs constructor is removed. The first argument of the NAbs
constructor now directly represents the lambda arguments.
* the select and the hasattr operator are moved into NExpr because
they are special (they only accept a selector as second argument, and
select also supports 'or')
The list of operators is now in Types.hs and Parser.hs re-uses that list
to build the parser. This is required because the pretty printer and
parser both need access to operator precedences.
Parser and evaluator also support dynamic attributes and attributes with
dots now. As an example, `let b.a = 3; b.c = { e = {}; }; b.c.e.${"f"} =
4; in b` is parsed and evaluated correctly. As a side effect, NSym
values now don't evaluate to themselves anymore, but instead to the
value retrieved by looking up the variable in the current environment.
Support for evaluating `inherit` bindings was removed because it was
broken before (`{ inherit a; }` would evaluate to a set where the
attribute `a` had the value `NSym a`, not the value of `a`).
The manual Show instances for the AST were replaced by derived
ones, because the manual instances often resulted in output were it was
difficult to determine the missing parentheses.
2014-08-15 22:11:54 +02:00
|
|
|
, Plain " g"
|
|
|
|
]
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "\"\\${a}\"" $ mkStr "${a}"
|
antiquotes + improve pretty printer + restructure
This commit improves the pretty printing and adds support for
antiquotes. It also fixes an issue with the parser that caused `[if true
then false else true]` to parse successfully, even though that is not a
valid nix expression.
The pretty printer now produces a lot more readable output and also
supports operator precedences.
The changes to the AST are:
* strings are no longer atomic, because they may contain other
expressions in the form of antiquotes. For strings, the new type
NString is introduced and the constructor NStr is added to NExprF
* the unused NVar constructor of NExprF is removed
* operators are now represented uniformly so that the pretty printer
can lookup information about operators (in particular, associativity
and precedence)
* the NArgs constructor is removed. The first argument of the NAbs
constructor now directly represents the lambda arguments.
* the select and the hasattr operator are moved into NExpr because
they are special (they only accept a selector as second argument, and
select also supports 'or')
The list of operators is now in Types.hs and Parser.hs re-uses that list
to build the parser. This is required because the pretty printer and
parser both need access to operator precedences.
Parser and evaluator also support dynamic attributes and attributes with
dots now. As an example, `let b.a = 3; b.c = { e = {}; }; b.c.e.${"f"} =
4; in b` is parsed and evaluated correctly. As a side effect, NSym
values now don't evaluate to themselves anymore, but instead to the
value retrieved by looking up the variable in the current environment.
Support for evaluating `inherit` bindings was removed because it was
broken before (`{ inherit a; }` would evaluate to a set where the
attribute `a` had the value `NSym a`, not the value of `a`).
The manual Show instances for the AST were replaced by derived
ones, because the manual instances often resulted in output were it was
difficult to determine the missing parentheses.
2014-08-15 22:11:54 +02:00
|
|
|
assertParseFail "\"a"
|
|
|
|
assertParseFail "${true}"
|
|
|
|
assertParseFail "\"${true\""
|
|
|
|
|
2014-08-16 01:31:41 +02:00
|
|
|
case_select = do
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "a . e .di. f" $ Fix $ NSelect (mkSym "a")
|
2018-04-17 21:46:41 +02:00
|
|
|
(StaticKey "e" Nothing :| [StaticKey "di" Nothing, StaticKey "f" Nothing])
|
2014-08-16 01:31:41 +02:00
|
|
|
Nothing
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "a.e . d or null" $ Fix $ NSelect (mkSym "a")
|
2018-04-17 21:46:41 +02:00
|
|
|
(StaticKey "e" Nothing :| [StaticKey "d" Nothing])
|
2014-08-16 01:31:41 +02:00
|
|
|
(Just mkNull)
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "{}.\"\"or null" $ Fix $ NSelect (Fix (NSet []))
|
2018-04-17 21:46:41 +02:00
|
|
|
(DynamicKey (Plain "") :| []) (Just mkNull)
|
2018-04-10 20:22:54 +02:00
|
|
|
assertParseText "{ a = [1]; }.a or [2] ++ [3]" $ Fix $ NBinary NConcat
|
|
|
|
(Fix (NSelect
|
2018-04-17 21:46:41 +02:00
|
|
|
(Fix (NSet [NamedVar (StaticKey "a" Nothing :| [])
|
2018-04-10 20:22:54 +02:00
|
|
|
(Fix (NList [Fix (NConstant (NInt 1))]))]))
|
2018-04-17 21:46:41 +02:00
|
|
|
(StaticKey "a" Nothing :| [])
|
2018-04-10 20:22:54 +02:00
|
|
|
(Just (Fix (NList [Fix (NConstant (NInt 2))])))))
|
|
|
|
(Fix (NList [Fix (NConstant (NInt 3))]))
|
2014-08-16 01:31:41 +02:00
|
|
|
|
2014-08-16 01:17:01 +02:00
|
|
|
case_select_path = do
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "f ./." $ Fix $ NBinary NApp (mkSym "f") (mkPath False "./.")
|
|
|
|
assertParseText "f.b ../a" $ Fix $ NBinary NApp select (mkPath False "../a")
|
|
|
|
assertParseText "{}./def" $ Fix $ NBinary NApp (Fix (NSet [])) (mkPath False "./def")
|
|
|
|
assertParseText "{}.\"\"./def" $ Fix $ NBinary NApp
|
2018-04-17 21:46:41 +02:00
|
|
|
(Fix $ NSelect (Fix (NSet [])) (DynamicKey (Plain "") :| []) Nothing)
|
2014-08-30 10:10:28 +02:00
|
|
|
(mkPath False "./def")
|
2014-08-16 01:17:01 +02:00
|
|
|
where select = Fix $ NSelect (mkSym "f") (mkSelector "b") Nothing
|
2014-08-16 00:16:12 +02:00
|
|
|
|
2014-08-16 11:47:45 +02:00
|
|
|
case_fun_app = do
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "f a b" $ Fix $ NBinary NApp (Fix $ NBinary NApp (mkSym "f") (mkSym "a")) (mkSym "b")
|
|
|
|
assertParseText "f a.x or null" $ Fix $ NBinary NApp (mkSym "f") $ Fix $
|
2014-08-16 11:47:45 +02:00
|
|
|
NSelect (mkSym "a") (mkSelector "x") (Just mkNull)
|
|
|
|
assertParseFail "f if true then null else null"
|
|
|
|
|
2014-08-18 22:54:20 +02:00
|
|
|
case_indented_string = do
|
2018-04-11 21:06:14 +02:00
|
|
|
assertParseText "''a''" $ mkIndentedStr 0 "a"
|
|
|
|
assertParseText "''\n foo\n bar''" $ mkIndentedStr 2 "foo\nbar"
|
|
|
|
assertParseText "'' ''" $ mkIndentedStr 0 ""
|
|
|
|
assertParseText "'''''''" $ mkIndentedStr 0 "''"
|
|
|
|
assertParseText "'' ${null}\n a${null}''" $ Fix $ NStr $ Indented 3
|
2014-08-18 22:54:20 +02:00
|
|
|
[ Antiquoted mkNull
|
|
|
|
, Plain "\na"
|
|
|
|
, Antiquoted mkNull
|
|
|
|
]
|
|
|
|
assertParseFail "'''''"
|
|
|
|
assertParseFail "'' '"
|
|
|
|
|
2018-04-10 02:12:16 +02:00
|
|
|
case_indented_string_escape = assertParseText
|
2014-08-18 22:54:20 +02:00
|
|
|
"'' ''\\n ''\\t ''\\\\ ''${ \\ \\n ' ''' ''" $
|
2018-04-11 21:06:14 +02:00
|
|
|
mkIndentedStr 1 "\n \t \\ ${ \\ \\n ' '' "
|
2014-08-18 22:54:20 +02:00
|
|
|
|
2014-08-18 23:06:57 +02:00
|
|
|
case_operator_fun_app = do
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "a ++ b" $ mkOper2 NConcat (mkSym "a") (mkSym "b")
|
|
|
|
assertParseText "a ++ f b" $ mkOper2 NConcat (mkSym "a") $ Fix $ NBinary NApp
|
2014-08-19 19:20:17 +02:00
|
|
|
(mkSym "f") (mkSym "b")
|
|
|
|
|
|
|
|
case_operators = do
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "1 + 2 - 3" $ mkOper2 NMinus
|
2014-08-19 19:20:17 +02:00
|
|
|
(mkOper2 NPlus (mkInt 1) (mkInt 2)) (mkInt 3)
|
|
|
|
assertParseFail "1 + if true then 1 else 2"
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "1 + (if true then 2 else 3)" $ mkOper2 NPlus (mkInt 1) $ Fix $ NIf
|
2014-08-19 19:20:17 +02:00
|
|
|
(mkBool True) (mkInt 2) (mkInt 3)
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "{ a = 3; } // rec { b = 4; }" $ mkOper2 NUpdate
|
2016-01-24 00:18:41 +01:00
|
|
|
(Fix $ NSet [NamedVar (mkSelector "a") (mkInt 3)])
|
|
|
|
(Fix $ NRecSet [NamedVar (mkSelector "b") (mkInt 4)])
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "--a" $ mkOper NNeg $ mkOper NNeg $ mkSym "a"
|
|
|
|
assertParseText "a - b - c" $ mkOper2 NMinus
|
2014-08-27 22:05:35 +02:00
|
|
|
(mkOper2 NMinus (mkSym "a") (mkSym "b")) $
|
|
|
|
mkSym "c"
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText "foo<bar" $ mkOper2 NLt (mkSym "foo") (mkSym "bar")
|
2014-08-30 10:10:28 +02:00
|
|
|
assertParseFail "+ 3"
|
|
|
|
assertParseFail "foo +"
|
2014-08-18 23:06:57 +02:00
|
|
|
|
2014-08-27 23:54:31 +02:00
|
|
|
case_comments = do
|
|
|
|
Success expected <- parseNixFile "data/let.nix"
|
|
|
|
assertParseFile "let-comments-multiline.nix" expected
|
|
|
|
assertParseFile "let-comments.nix" expected
|
|
|
|
|
2018-04-12 23:22:55 +02:00
|
|
|
case_select_or_precedence =
|
|
|
|
assertParsePrint [i|let
|
|
|
|
matchDef = def: matcher:
|
|
|
|
v: let
|
|
|
|
case = builtins.head (builtins.attrNames v);
|
|
|
|
in (matcher.case or def case) (v.case);
|
|
|
|
in null|] [i|let
|
2018-04-19 09:21:30 +02:00
|
|
|
matchDef = def:
|
|
|
|
matcher:
|
|
|
|
v:
|
|
|
|
let
|
|
|
|
case = builtins.head (builtins.attrNames v);
|
|
|
|
in (matcher.case or def) case (v.case);
|
2018-04-12 23:22:55 +02:00
|
|
|
in null|]
|
|
|
|
|
|
|
|
case_select_or_precedence2 =
|
|
|
|
assertParsePrint [i|let
|
|
|
|
matchDef = def: matcher:
|
|
|
|
v: let
|
|
|
|
case = builtins.head (builtins.attrNames v);
|
|
|
|
in (matcher.case or null.foo) (v.case);
|
|
|
|
in null|] [i|let
|
2018-04-19 09:21:30 +02:00
|
|
|
matchDef = def:
|
|
|
|
matcher:
|
|
|
|
v:
|
|
|
|
let
|
|
|
|
case = builtins.head (builtins.attrNames v);
|
|
|
|
in (matcher.case or null).foo (v.case);
|
2018-04-12 23:22:55 +02:00
|
|
|
in null|]
|
|
|
|
|
2014-08-03 14:17:43 +02:00
|
|
|
tests :: TestTree
|
|
|
|
tests = $testGroupGenerator
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2018-04-06 08:16:55 +02:00
|
|
|
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseText :: Text -> NExpr -> Assertion
|
|
|
|
assertParseText str expected = case parseNixText str of
|
2018-04-06 08:16:55 +02:00
|
|
|
Success actual ->
|
2018-04-10 02:12:16 +02:00
|
|
|
assertEqual ("When parsing " ++ unpack str)
|
2018-04-06 08:16:55 +02:00
|
|
|
(stripPositionInfo expected) (stripPositionInfo actual)
|
|
|
|
Failure err ->
|
2018-04-10 02:12:16 +02:00
|
|
|
assertFailure $ "Unexpected error parsing `" ++ unpack str ++ "':\n" ++ show err
|
2014-08-03 16:45:34 +02:00
|
|
|
|
2014-08-27 23:54:31 +02:00
|
|
|
assertParseFile :: FilePath -> NExpr -> Assertion
|
|
|
|
assertParseFile file expected = do
|
|
|
|
res <- parseNixFile $ "data/" ++ file
|
|
|
|
case res of
|
2018-04-06 08:16:55 +02:00
|
|
|
Success actual -> assertEqual ("Parsing data file " ++ file)
|
|
|
|
(stripPositionInfo expected) (stripPositionInfo actual)
|
|
|
|
Failure err ->
|
|
|
|
assertFailure $ "Unexpected error parsing data file `"
|
|
|
|
++ file ++ "':\n" ++ show err
|
2014-08-27 23:54:31 +02:00
|
|
|
|
2018-04-10 02:12:16 +02:00
|
|
|
assertParseFail :: Text -> Assertion
|
|
|
|
assertParseFail str = case parseNixText str of
|
2014-08-03 16:45:34 +02:00
|
|
|
Failure _ -> return ()
|
2018-04-06 08:16:55 +02:00
|
|
|
Success r ->
|
|
|
|
assertFailure $ "Unexpected success parsing `"
|
2018-04-10 02:12:16 +02:00
|
|
|
++ unpack str ++ ":\nParsed value: " ++ show r
|
2018-04-12 23:22:55 +02:00
|
|
|
|
|
|
|
-- assertRoundTrip :: Text -> Assertion
|
|
|
|
-- assertRoundTrip src = assertParsePrint src src
|
|
|
|
|
|
|
|
assertParsePrint :: Text -> Text -> Assertion
|
|
|
|
assertParsePrint src expect =
|
|
|
|
let Success expr = parseNixTextLoc src
|
|
|
|
result = displayS
|
|
|
|
. renderPretty 0.4 80
|
|
|
|
. prettyNix
|
|
|
|
. stripAnnotation
|
|
|
|
$ expr
|
|
|
|
in assertEqual "" expect (pack (result ""))
|