Skip to content

Commit

Permalink
Move SyntaxSegment.decode into own module
Browse files Browse the repository at this point in the history
  • Loading branch information
hojberg committed Jun 28, 2024
1 parent 8d92613 commit 5f696c8
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 152 deletions.
154 changes: 4 additions & 150 deletions src/Code/Syntax.elm
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ module Code.Syntax exposing

import Code.Definition.Reference as Reference exposing (Reference)
import Code.FullyQualifiedName as FQN exposing (FQN)
import Code.Hash as Hash
import Code.HashQualified as HQ
import Code.Syntax.SyntaxHelp as SyntaxHelp
import Code.Syntax.SyntaxSegment exposing (..)
import Code.Syntax.SyntaxSegment as SyntaxSegment exposing (..)
import Html exposing (Html, span, text)
import Html.Attributes exposing (class)
import Html.Events exposing (onMouseEnter, onMouseLeave)
import Json.Decode as Decode exposing (andThen, at, field)
import Json.Decode.Extra exposing (when)
import Json.Decode as Decode exposing (andThen)
import Lib.Util as Util
import List.Nonempty as NEL
import UI.Click as Click exposing (Click)
Expand Down Expand Up @@ -400,155 +398,11 @@ view linked (Syntax segments) =
-- JSON DECODE


simpleSyntaxTypeFromString : String -> SyntaxType
simpleSyntaxTypeFromString rawType =
case rawType of
"NumericLiteral" ->
NumericLiteral

"TextLiteral" ->
TextLiteral

"BytesLiteral" ->
BytesLiteral

"CharLiteral" ->
CharLiteral

"BooleanLiteral" ->
BooleanLiteral

"Blank" ->
Blank

"Var" ->
Var

"AbilityBraces" ->
AbilityBraces

"ControlKeyword" ->
ControlKeyword

"TypeOperator" ->
TypeOperator

"BindingEquals" ->
BindingEquals

"TypeAscriptionColon" ->
TypeAscriptionColon

"DataTypeKeyword" ->
DataTypeKeyword

"DataTypeParams" ->
DataTypeParams

"Unit" ->
Unit

"DataTypeModifier" ->
DataTypeModifier

"UseKeyword" ->
UseKeyword

"UsePrefix" ->
UsePrefix

"UseSuffix" ->
UseSuffix

"DelayForceChar" ->
DelayForceChar

"DelimiterChar" ->
DelimiterChar

"Parenthesis" ->
Parenthesis

"LinkKeyword" ->
LinkKeyword

"DocDelimiter" ->
DocDelimiter

"DocKeyword" ->
DocKeyword

_ ->
Blank


decodeOp : Decode.Decoder SyntaxType
decodeOp =
let
decodeOpTag =
at [ "annotation", "contents", "tag" ] Decode.string
in
Decode.map
Op
(Decode.oneOf
[ when decodeOpTag ((==) "Cons") (Decode.succeed Cons)
, when decodeOpTag ((==) "Snoc") (Decode.succeed Snoc)
, when decodeOpTag ((==) "Concat") (Decode.succeed Concat)
]
)


decodeTag : Decode.Decoder String
decodeTag =
Decode.oneOf
[ at [ "annotation", "tag" ] Decode.string
, Decode.succeed "Blank"
]


decodeSyntaxSegment : Decode.Decoder SyntaxSegment
decodeSyntaxSegment =
let
hashToReference hash fqn =
if Hash.isDataConstructorHash hash then
DataConstructorReference hash fqn

else if Hash.isAbilityConstructorHash hash then
AbilityConstructorReference hash fqn

else
TermReference hash fqn

decodeReference =
Decode.map2 hashToReference
(at [ "annotation", "contents" ] Hash.decode)
(Decode.maybe (field "segment" FQN.decode))

decodeTypeReference =
Decode.map2 TypeReference
(at [ "annotation", "contents" ] Hash.decode)
(Decode.maybe (field "segment" FQN.decode))

decodeHashQualifier =
Decode.map HashQualifier (at [ "annotation", "contents" ] Decode.string)
in
Decode.map2 SyntaxSegment
(Decode.oneOf
[ when decodeTag ((==) "TermReference") decodeReference
, when decodeTag ((==) "TypeReference") decodeTypeReference
, when decodeTag ((==) "Op") decodeOp
, when decodeTag ((==) "HashQualifier") decodeHashQualifier
, decodeTag |> andThen (simpleSyntaxTypeFromString >> Decode.succeed)
]
)
(field "segment" Decode.string)


decodeSingleton : Decode.Decoder Syntax
decodeSingleton =
Decode.map Syntax (Decode.map NEL.fromElement decodeSyntaxSegment)
Decode.map Syntax (Decode.map NEL.fromElement SyntaxSegment.decode)


decode : Decode.Decoder Syntax
decode =
Util.decodeNonEmptyList decodeSyntaxSegment |> andThen (Syntax >> Decode.succeed)
Util.decodeNonEmptyList SyntaxSegment.decode |> andThen (Syntax >> Decode.succeed)
154 changes: 152 additions & 2 deletions src/Code/Syntax/SyntaxSegment.elm
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module Code.Syntax.SyntaxSegment exposing (..)

import Code.FullyQualifiedName exposing (FQN)
import Code.Hash exposing (Hash)
import Code.FullyQualifiedName as FQN exposing (FQN)
import Code.Hash as Hash exposing (Hash)
import Json.Decode as Decode exposing (andThen, at, field)
import Json.Decode.Extra exposing (when)


type SyntaxSegment
Expand Down Expand Up @@ -59,3 +61,151 @@ type SyntaxType
| DocDelimiter
-- the 'include' in @[include], etc
| DocKeyword



-- JSON DECODE


simpleSyntaxTypeFromString : String -> SyntaxType
simpleSyntaxTypeFromString rawType =
case rawType of
"NumericLiteral" ->
NumericLiteral

"TextLiteral" ->
TextLiteral

"BytesLiteral" ->
BytesLiteral

"CharLiteral" ->
CharLiteral

"BooleanLiteral" ->
BooleanLiteral

"Blank" ->
Blank

"Var" ->
Var

"AbilityBraces" ->
AbilityBraces

"ControlKeyword" ->
ControlKeyword

"TypeOperator" ->
TypeOperator

"BindingEquals" ->
BindingEquals

"TypeAscriptionColon" ->
TypeAscriptionColon

"DataTypeKeyword" ->
DataTypeKeyword

"DataTypeParams" ->
DataTypeParams

"Unit" ->
Unit

"DataTypeModifier" ->
DataTypeModifier

"UseKeyword" ->
UseKeyword

"UsePrefix" ->
UsePrefix

"UseSuffix" ->
UseSuffix

"DelayForceChar" ->
DelayForceChar

"DelimiterChar" ->
DelimiterChar

"Parenthesis" ->
Parenthesis

"LinkKeyword" ->
LinkKeyword

"DocDelimiter" ->
DocDelimiter

"DocKeyword" ->
DocKeyword

_ ->
Blank


decodeOp : Decode.Decoder SyntaxType
decodeOp =
let
decodeOpTag =
at [ "annotation", "contents", "tag" ] Decode.string
in
Decode.map
Op
(Decode.oneOf
[ when decodeOpTag ((==) "Cons") (Decode.succeed Cons)
, when decodeOpTag ((==) "Snoc") (Decode.succeed Snoc)
, when decodeOpTag ((==) "Concat") (Decode.succeed Concat)
]
)


decodeTag : Decode.Decoder String
decodeTag =
Decode.oneOf
[ at [ "annotation", "tag" ] Decode.string
, Decode.succeed "Blank"
]


decode : Decode.Decoder SyntaxSegment
decode =
let
hashToReference hash fqn =
if Hash.isDataConstructorHash hash then
DataConstructorReference hash fqn

else if Hash.isAbilityConstructorHash hash then
AbilityConstructorReference hash fqn

else
TermReference hash fqn

decodeReference =
Decode.map2 hashToReference
(at [ "annotation", "contents" ] Hash.decode)
(Decode.maybe (field "segment" FQN.decode))

decodeTypeReference =
Decode.map2 TypeReference
(at [ "annotation", "contents" ] Hash.decode)
(Decode.maybe (field "segment" FQN.decode))

decodeHashQualifier =
Decode.map HashQualifier (at [ "annotation", "contents" ] Decode.string)
in
Decode.map2 SyntaxSegment
(Decode.oneOf
[ when decodeTag ((==) "TermReference") decodeReference
, when decodeTag ((==) "TypeReference") decodeTypeReference
, when decodeTag ((==) "Op") decodeOp
, when decodeTag ((==) "HashQualifier") decodeHashQualifier
, decodeTag |> andThen (simpleSyntaxTypeFromString >> Decode.succeed)
]
)
(field "segment" Decode.string)

0 comments on commit 5f696c8

Please sign in to comment.