2017-01-07 20:29:26 +03:00
|
|
|
module Main exposing (main)
|
|
|
|
|
2017-01-07 22:58:41 +03:00
|
|
|
import Result
|
2017-01-07 20:29:26 +03:00
|
|
|
import Ast
|
|
|
|
import Ast.Expression exposing (..)
|
|
|
|
import Ast.Statement exposing (..)
|
2017-01-07 22:58:41 +03:00
|
|
|
import Html exposing (Html, p, div, li, ul, pre, textarea, text)
|
2017-01-16 00:57:03 +03:00
|
|
|
import Html.Attributes exposing (id, class)
|
2017-01-07 20:29:26 +03:00
|
|
|
import Html.Events exposing (..)
|
|
|
|
import Json.Decode as JD
|
2017-01-16 01:24:37 +03:00
|
|
|
import Types
|
2017-01-08 13:44:31 +03:00
|
|
|
|
|
|
|
|
|
|
|
-- Rules
|
|
|
|
|
2017-01-16 02:16:28 +03:00
|
|
|
import NoUnannotatedFunction
|
2017-01-08 13:44:31 +03:00
|
|
|
import NoDebug
|
|
|
|
import NoExposingEverything
|
2017-01-17 01:32:06 +03:00
|
|
|
import NoUnusedVariables
|
2017-01-07 20:29:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
type Msg
|
|
|
|
= Replace String
|
|
|
|
|
|
|
|
|
|
|
|
init : String
|
|
|
|
init =
|
|
|
|
"""module Main exposing (..)
|
|
|
|
|
|
|
|
f : Int -> Int
|
2017-01-07 23:17:01 +03:00
|
|
|
f x = x Debug.log 1
|
2017-01-07 20:29:26 +03:00
|
|
|
|
2017-01-07 23:17:01 +03:00
|
|
|
a : a -> a
|
|
|
|
a = Debug.log "foo" x
|
2017-01-07 20:29:26 +03:00
|
|
|
|
2017-01-07 23:17:01 +03:00
|
|
|
h = f << Debug.log
|
2017-01-07 20:29:26 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
update : Msg -> String -> String
|
|
|
|
update action model =
|
|
|
|
case action of
|
|
|
|
Replace m ->
|
|
|
|
m
|
|
|
|
|
|
|
|
|
|
|
|
withChild : a -> List (Html Msg) -> Html Msg
|
|
|
|
withChild title children =
|
|
|
|
li []
|
|
|
|
[ pre [] [ text <| toString title ]
|
|
|
|
, ul [] children
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
expression : Expression -> Html Msg
|
|
|
|
expression e =
|
|
|
|
case e of
|
|
|
|
List es ->
|
|
|
|
withChild e (List.map expression es)
|
|
|
|
|
|
|
|
Application e1 e2 ->
|
|
|
|
withChild e
|
|
|
|
[ expression e1
|
|
|
|
, expression e2
|
|
|
|
]
|
|
|
|
|
|
|
|
e ->
|
|
|
|
li [] [ pre [] [ text <| toString e ] ]
|
|
|
|
|
|
|
|
|
|
|
|
statement : Statement -> Html Msg
|
|
|
|
statement s =
|
|
|
|
case s of
|
|
|
|
FunctionDeclaration _ _ e ->
|
|
|
|
withChild s [ expression e ]
|
|
|
|
|
|
|
|
s ->
|
|
|
|
li [] [ pre [] [ text <| toString s ] ]
|
|
|
|
|
|
|
|
|
2017-01-07 22:58:41 +03:00
|
|
|
tree : Result a ( b, c, List Statement ) -> Html Msg
|
|
|
|
tree ast =
|
|
|
|
case ast of
|
2017-01-07 20:29:26 +03:00
|
|
|
Ok ( _, _, statements ) ->
|
|
|
|
ul [] (List.map statement statements)
|
|
|
|
|
|
|
|
err ->
|
|
|
|
div [] [ text <| toString err ]
|
|
|
|
|
|
|
|
|
2017-01-16 01:24:37 +03:00
|
|
|
rules : List (String -> List Types.Error)
|
|
|
|
rules =
|
2017-01-16 02:16:28 +03:00
|
|
|
[ NoDebug.rule
|
2017-01-16 01:24:37 +03:00
|
|
|
, NoExposingEverything.rule
|
2017-01-16 02:16:28 +03:00
|
|
|
, NoUnannotatedFunction.rule
|
2017-01-17 01:32:06 +03:00
|
|
|
, NoUnusedVariables.rule
|
2017-01-16 01:24:37 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2017-01-10 00:12:37 +03:00
|
|
|
lint : String -> Html Msg
|
|
|
|
lint source =
|
2017-01-07 22:58:41 +03:00
|
|
|
let
|
|
|
|
errors =
|
2017-01-16 02:16:28 +03:00
|
|
|
List.concatMap (\rule -> rule source) rules
|
2017-01-17 01:38:05 +03:00
|
|
|
|
|
|
|
messages =
|
|
|
|
if List.isEmpty errors then
|
|
|
|
[ "No issues here." ]
|
|
|
|
else
|
|
|
|
List.map (\err -> err.rule ++ ": " ++ err.message) errors
|
2017-01-07 22:58:41 +03:00
|
|
|
in
|
2017-01-16 01:24:37 +03:00
|
|
|
div []
|
|
|
|
(List.map
|
2017-01-17 01:38:05 +03:00
|
|
|
(\message -> p [] [ text message ])
|
|
|
|
messages
|
2017-01-16 01:24:37 +03:00
|
|
|
)
|
2017-01-07 22:58:41 +03:00
|
|
|
|
|
|
|
|
2017-01-07 20:29:26 +03:00
|
|
|
view : String -> Html Msg
|
|
|
|
view model =
|
2017-01-10 00:12:37 +03:00
|
|
|
div [ id "wrapper" ]
|
2017-01-16 00:57:03 +03:00
|
|
|
[ div [ id "left" ]
|
|
|
|
[ p [ class "title" ] [ text "Source code" ]
|
|
|
|
, textarea
|
|
|
|
[ id "input"
|
|
|
|
, on "input" (JD.map Replace targetValue)
|
|
|
|
]
|
|
|
|
[ text model ]
|
|
|
|
, div []
|
|
|
|
[ p [ class "title" ] [ text "Linting errors" ]
|
|
|
|
, div [ id "lint" ]
|
|
|
|
[ lint model
|
|
|
|
]
|
|
|
|
]
|
2017-01-10 00:12:37 +03:00
|
|
|
]
|
|
|
|
, div [ id "right" ]
|
2017-01-16 00:57:03 +03:00
|
|
|
[ p [ class "title" ] [ text "AST" ]
|
|
|
|
, p [ id "ast" ] [ tree <| Ast.parse model ]
|
2017-01-07 22:58:41 +03:00
|
|
|
]
|
2017-01-10 00:12:37 +03:00
|
|
|
]
|
2017-01-07 20:29:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
main : Program Never String Msg
|
|
|
|
main =
|
|
|
|
Html.beginnerProgram
|
|
|
|
{ model = init
|
|
|
|
, update = update
|
|
|
|
, view = view
|
|
|
|
}
|