Skip to content

Commit 975f645

Browse files
authored
Add text source (#57)
* Add text source * Add newline and tab to prelude ch namespace * Undo accidental interpolation in prelude doc
1 parent 8810208 commit 975f645

File tree

6 files changed

+72
-4
lines changed

6 files changed

+72
-4
lines changed

lib/prelude.eu

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,13 @@ zero?(n): n = 0
295295
##
296296
## Text and regexes
297297
##
298+
` { export: :suppress
299+
doc: "We don't support the usual string escapes so these make it a little more convenient to use interpolation instead.."}
300+
ch: {
301+
n: "
302+
"
303+
t: " "
304+
}
298305

299306
` :suppress
300307
str: {
@@ -439,20 +446,35 @@ assertions: {
439446
export: :suppress
440447
associates: :left
441448
precedence: :meta }
442-
(e //= v): e with-meta({ assert: __EQ(v)}) assertions.check
449+
(e //= v): e with-meta({ assert: (= v)}) assertions.check
443450

444451
` { doc: "`e //=> v` - add metadata to assert expression `e` evaluates to `v` and return value of `e`."
445452
export: :suppress
446453
associates: :left
447454
precedence: :meta }
448-
(e //=> v): e with-meta({ assert: __EQ(v)}) assertions.checked
455+
(e //=> v): e with-meta({ assert: (= v)}) assertions.checked
449456

450457
` { doc: "`e //=? f` - add metadata to assert expression `e` satisfies function `f` and return value of `e`."
451458
export: :suppress
452459
associates: :left
453460
precedence: :meta }
454461
(e //=? f): e with-meta({ assert: f}) assertions.checked
455462

463+
` { doc: "`e //!? f` - add metadata to assert expression `e` satisfies function `f` and return value of `e`."
464+
export: :suppress
465+
associates: :left
466+
precedence: :meta }
467+
(e //!? f): e with-meta({ assert: complement(f)}) assertions.checked
468+
469+
` { doc: "`e //! f` - add metadata to assert expression `e` is true and return value of `e`."
470+
export: :suppress
471+
precedence: :meta }
472+
(e //!): e with-meta({ assert: (= true) }) assertions.checked
473+
474+
` { doc: "`e //!! f` - add metadata to assert expression `e` is false and return value of `e`."
475+
export: :suppress
476+
precedence: :meta }
477+
(e //!!): e with-meta({ assert: (= false) }) assertions.checked
456478

457479
#
458480
# List library functions, maps and folds
@@ -655,7 +677,7 @@ select-items(f, b): b elements filter(f)
655677
match-select-values(re, b): b select-items(by-key-match(re)) map(value)
656678

657679
` "`select-values(p?, b)` - return items from block `b` where values match predicate `p?`"
658-
select-values(p?, b): b map(value) filter(p?)
680+
select-values(p?, b): b values filter(p?)
659681

660682
` :suppress
661683
_block: {

package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ copyright: "2018 Greg Hawkins"
88

99
extra-source-files:
1010
- README.md
11+
- lib/prelude.eu
1112

1213
synopsis: Haskell implementation of Eucalypt tooling and language
1314
description: Please see the README on Github at <https://github.com/curvelogic/eucalypt-hs#readme>

src/Eucalypt/Driver/Core.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import Eucalypt.Driver.Lib (getResource)
3939
import Eucalypt.Driver.Options (EucalyptOptions(..))
4040
import Eucalypt.Reporting.Error (EucalyptError(..))
4141
import Eucalypt.Source.Error (DataParseException(..))
42+
import Eucalypt.Source.TextSource
4243
import Eucalypt.Source.TomlSource
4344
import Eucalypt.Source.YamlSource
4445
import Eucalypt.Syntax.Ast (Unit)
@@ -136,6 +137,7 @@ loadUnit i@(Input locator name format) = do
136137
coreUnit <-
137138
liftIO $
138139
case format of
140+
"text" -> textDataToCore source
139141
"toml" -> tomlDataToCore source
140142
"yaml" -> activeYamlToCore source
141143
"json" -> yamlDataToCore source
@@ -157,7 +159,8 @@ loadUnit i@(Input locator name format) = do
157159
case r of
158160
Left e -> (return . Left . Source) e
159161
Right core -> (return . Right . maybeApplyName . dataUnit) core
160-
tomlDataToCore text = parseTomlData text >>= (return . Right <$> dataUnit)
162+
textDataToCore text = parseTextLines text >>= (return . Right . maybeApplyName <$> dataUnit)
163+
tomlDataToCore text = parseTomlData text >>= (return . Right .maybeApplyName <$> dataUnit)
161164
activeYamlToCore text = do
162165
r <- try (parseYamlExpr text) :: IO (Either DataParseException CoreExpr)
163166
case r of

src/Eucalypt/Source/TextSource.hs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{-|
2+
Module : Eucalypt.Source.TextSource
3+
Description : Ingest text lines into core syntax
4+
Copyright : (c) Greg Hawkins, 2018
5+
License :
6+
Maintainer : greg@curvelogic.co.uk
7+
Stability : experimental
8+
-}
9+
module Eucalypt.Source.TextSource where
10+
11+
import qualified Data.ByteString as BS
12+
import qualified Data.Text as T
13+
import Data.Text.Encoding (decodeUtf8)
14+
import Eucalypt.Core.SourceMap
15+
import Eucalypt.Core.Syn
16+
17+
parseTextLines :: BS.ByteString -> IO CoreExpr
18+
parseTextLines =
19+
return . anon corelist . map (anon str . T.unpack) . T.lines . decodeUtf8

src/Eucalypt/Syntax/Input.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ inferFormat loc =
7272
extToFormat ext =
7373
case ext of
7474
".json" -> Just "json"
75+
".txt" -> Just "text"
7576
".toml" -> Just "toml"
7677
".yaml" -> Just "yaml"
7778
".yml" -> Just "yaml"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
3+
module Eucalypt.Source.TextSourceSpec
4+
( main
5+
, spec
6+
) where
7+
8+
import Eucalypt.Core.AnonSyn
9+
import Eucalypt.Source.TextSource
10+
import Data.Text
11+
import Data.Text.Encoding
12+
import Test.Hspec
13+
14+
main :: IO ()
15+
main = hspec spec
16+
17+
spec :: Spec
18+
spec =
19+
describe "Text source" $
20+
it "reads as list" $
21+
parseTextLines (encodeUtf8 $ pack "one\ntwo\nthree\nξ") `shouldReturn`
22+
corelist [str "one", str "two", str "three", str "ξ"]

0 commit comments

Comments
 (0)