Extension and grammar

This commit is contained in:
Kirill Andreev 2020-08-17 16:29:42 +04:00
parent 26d11eea19
commit 1a948bcae7
No known key found for this signature in database
GPG Key ID: CF7DA79DE4785A47
5 changed files with 52 additions and 48 deletions

View File

@ -23,52 +23,47 @@ module.exports = grammar({
$.include,
),
include: $ => seq('#include', field("filename", $.String)),
include: $ => seq(
'#include',
field("filename", $.String)
),
_attribute: $ => /\[@@[a-z]+\]/,
argument: $ => seq(
"(",
field("argPattern", $._pattern),
":",
field("annotExpr", $.type_expr),
")"
),
let_decl: $ => seq(
"let",
field("binder", $._binder),
field("name", $._binder),
optional(seq(
":",
field("annotExpr", $.type_expr)
field("type", $.type_expr)
)),
"=",
field("letExpr",$._let_expr),
field("body",$._program),
repeat(field("attribute", $._attribute))
),
_binder: $ => choice(
field("letBinds", $.let_binds),
field("letPat", $._pattern)
$.func_header,
$._pattern
),
//========== EXPR ============
_let_expr: $ => choice(
_program: $ => choice(
$.let_expr1,
$._expr
),
let_binds: $ => prec(1, seq(
func_header: $ => prec(1, seq(
optional(field("recursive", "rec")),
field("bindName", $.Name),
repeat(field("bindArgument", $.argument))
field("name", $.Name),
repeat(field("arg", $.paren_pattern))
)),
let_expr1: $ => seq(
$.let_decl,
"in",
field("innerExpr", $._let_expr)
field("innerExpr", $._program)
),
// [1;2]
@ -171,10 +166,10 @@ module.exports = grammar({
"if",
field("condition", $._expr),
"then",
field("thenBranch", $._let_expr),
field("thenBranch", $._program),
optional(seq(
"else",
field("elseBranch", $._let_expr)
field("elseBranch", $._program)
))
)),
@ -191,12 +186,12 @@ module.exports = grammar({
matching: $ => seq(
field("pattern", $._pattern),
"->",
field("matchingExpr", $._let_expr)
field("matchingExpr", $._program)
),
lambda_expr: $ => seq(
"fun",
repeat1(field("arg", $.argument)),
repeat1(field("arg", $.paren_pattern)),
"->",
field("body", $._expr)
),
@ -236,13 +231,13 @@ module.exports = grammar({
block_expr: $ => seq(
"begin",
sepBy(";", field("elem", $._let_expr)),
sepBy(";", field("elem", $._program)),
"end",
),
paren_expr: $ => seq(
"(",
field("innerExpr", $._let_expr),
field("innerExpr", $._program),
optional(seq(
":",
field("annotExpr", $.type_expr)

View File

@ -14,3 +14,16 @@ import Parser
import ParseTree
-- import Debug.Trace
example :: FilePath
example = "../../../src/test/contracts/address.mligo"
raw :: IO ()
raw = toParseTree (Path example)
>>= print . pp
-- sample :: IO ()
-- sample
-- = toParseTree (Path example)
-- >>= runParserM . recognise
-- >>= print . pp . fst

View File

@ -15,7 +15,7 @@ import ParseTree
-- import Debug.Trace
example :: FilePath
-- example :: FilePath
-- example = "../../../src/test/contracts/arithmetic.ligo"
-- example = "../../../src/test/contracts/address.ligo"
-- example = "../../../src/test/contracts/annotation.ligo"
@ -36,7 +36,6 @@ example :: FilePath
-- example = "../../../src/test/contracts/bytes_arithmetic.ligo"
-- example = "../../../src/test/contracts/chain_id.ligo"
-- example = "../../../src/test/contracts/closure-3.ligo"
example = "../../../src/test/contracts/coase.ligo"
sample' :: FilePath -> IO (LIGO Info)
sample' f
@ -49,16 +48,16 @@ source' f
= toParseTree (Path f)
>>= print . pp
sample :: IO ()
sample
= toParseTree (Path example)
>>= runParserM . recognise
>>= print . pp . fst
-- sample :: IO ()
-- sample
-- = toParseTree (Path example)
-- >>= runParserM . recognise
-- >>= print . pp . fst
source :: IO ()
source
= toParseTree (Path example)
>>= print . pp
-- source :: IO ()
-- source
-- = toParseTree (Path example)
-- >>= print . pp
recognise :: RawTree -> ParserM (LIGO Info)
recognise = descent (\_ -> error . show . pp) $ map usingScope

View File

@ -23,9 +23,9 @@ data UnsupportedExtension = UnsupportedExtension String
getExt :: MonadThrow m => FilePath -> m Ext
getExt path = do
case takeExtension path of
"religo" -> return Reason
"ligo" -> return Pascal
"mligo" -> return Caml
".religo" -> return Reason
".ligo" -> return Pascal
".mligo" -> return Caml
ext -> throwM $ UnsupportedExtension ext
onExt :: ElimExt a -> FilePath -> IO a

View File

@ -113,9 +113,7 @@ toParseTree = unsafeDebounce \fin -> do
} (srcPath fin)
parser <- ts_parser_new
-- True <- ts_parser_set_language parser tree_sitter_PascaLigo
True <- ts_parser_set_language parser language
src <- srcToBytestring fin
BS.useAsCStringLen src \(str, len) -> do
@ -166,7 +164,6 @@ toParseTree = unsafeDebounce \fin -> do
return $ make (range :> "" :> Nil, ParseTree
{ ptName = Text.pack ty
-- , ptChildren = fromList . fmap (Comment,) $ trees -- TODO
, ptChildren = trees
, ptSource = cutOut range src
})