Skip to content

Commit

Permalink
Store source location of (almost) everything (#2174)
Browse files Browse the repository at this point in the history
- Closes #2162 

This pr improves formatting of source files with comments.
The concrete ast now stores location information of almost all keywords.
We do not store location information of parentheses. Comments will be
pushed out of parentheses by the formatter.
E.g.
```
( -- comment
 f x)
```
will become
```
-- comment
(f x)
```

This only occurs if the comment appears just after the `(`. So the
following will be respected
```
(f --comment
x)
```

---------

Co-authored-by: Jonathan Cubides <jonathan.cubides@uib.no>
  • Loading branch information
janmasrovira and jonaprieto authored Jun 7, 2023
1 parent 216c1cd commit b7b0e10
Show file tree
Hide file tree
Showing 28 changed files with 645 additions and 287 deletions.
6 changes: 3 additions & 3 deletions examples/milestone/TicTacToe/Logic/Board.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ full _ := fail "full";

diagonals : List (List Square) → List (List Square);
diagonals ((a1 :: _ :: b1 :: nil)
:: (_ :: c :: _ :: nil)
:: (b2 :: _ :: a2 :: nil)
:: nil) :=
:: (_ :: c :: _ :: nil)
:: (b2 :: _ :: a2 :: nil)
:: nil) :=
(a1 :: c :: a2 :: nil) :: (b1 :: c :: b2 :: nil) :: nil;
diagonals _ := fail "diagonals";

Expand Down
4 changes: 2 additions & 2 deletions src/Juvix/Compiler/Abstract/Extra/Functions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ toApplicationArg p =
( ExpressionHole
( Hole
{ _holeId = error "hole with no id",
_holeLoc = error "hole with no location"
_holeKw = error "hole with no location"
}
)
)
Expand Down Expand Up @@ -337,6 +337,6 @@ freshHole = do
ExpressionHole
( Hole
{ _holeId = uid,
_holeLoc = error "freshHole with no location"
_holeKw = error "freshHole with no location"
}
)
2 changes: 1 addition & 1 deletion src/Juvix/Compiler/Abstract/Pretty/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ instance PrettyCode Application where
return $ l' <+> r'

instance PrettyCode Universe where
ppCode (Universe n _) = return $ kwType <+?> (pretty <$> n)
ppCode Universe {..} = return $ kwType <+?> (pretty <$> _universeLevel)

instance PrettyCode PatternArg where
ppCode (PatternArg i n p) = do
Expand Down
25 changes: 14 additions & 11 deletions src/Juvix/Compiler/Abstract/Translation/FromConcrete.hs
Original file line number Diff line number Diff line change
Expand Up @@ -529,18 +529,21 @@ goFunctionParameters ::
(Members '[Error ScoperError, Reader Pragmas, InfoTableBuilder] r) =>
FunctionParameters 'Scoped ->
Sem r (NonEmpty Abstract.FunctionParameter)
goFunctionParameters (FunctionParameters {..}) = do
goFunctionParameters FunctionParameters {..} = do
_paramType' <- goExpression _paramType
return $
fmap
( \param ->
Abstract.FunctionParameter
{ Abstract._paramType = _paramType',
Abstract._paramImplicit = _paramImplicit,
Abstract._paramName = goSymbol <$> param
}
)
(fromMaybe (pure Nothing) (nonEmpty _paramNames))
let mkParam param =
Abstract.FunctionParameter
{ Abstract._paramType = _paramType',
Abstract._paramImplicit = _paramImplicit,
Abstract._paramName = goSymbol <$> param
}
return . fromMaybe (pure (mkParam Nothing)) . nonEmpty $
mkParam . goFunctionParameter <$> _paramNames
where
goFunctionParameter :: FunctionParameter 'Scoped -> Maybe (SymbolType 'Scoped)
goFunctionParameter = \case
FunctionParameterName n -> Just n
FunctionParameterWildcard {} -> Nothing

goPatternApplication ::
(Members '[Error ScoperError, InfoTableBuilder] r) =>
Expand Down
8 changes: 4 additions & 4 deletions src/Juvix/Compiler/Asm/Keywords.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ where

import Juvix.Data.Keyword
import Juvix.Data.Keyword.All
( kwArg,
( delimSemicolon,
kwArg,
kwColon,
kwDollar,
kwFalse,
kwFun,
kwInductive,
kwRightArrow,
kwSemicolon,
kwStar,
kwTmp,
kwTrue,
Expand All @@ -28,10 +28,10 @@ allKeywordStrings = keywordsStrings allKeywords

allKeywords :: [Keyword]
allKeywords =
[ kwFun,
[ delimSemicolon,
kwFun,
kwInductive,
kwColon,
kwSemicolon,
kwStar,
kwRightArrow,
kwTrue,
Expand Down
6 changes: 3 additions & 3 deletions src/Juvix/Compiler/Asm/Translation/FromSource.hs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ statementFunction = do
_functionMaxTempStackHeight = -1
}
lift $ registerFunction fi0
mcode <- (kw kwSemicolon $> Nothing) <|> optional (braces parseCode)
mcode <- (kw delimSemicolon $> Nothing) <|> optional (braces parseCode)
let fi = fi0 {_functionCode = fromMaybe [] mcode}
case idt of
Just (IdentFwd _) -> do
Expand Down Expand Up @@ -155,7 +155,7 @@ statementInductive = do
_inductiveRepresentation = IndRepStandard
}
lift $ registerInductive ii
ctrs <- braces $ P.sepEndBy (constrDecl sym) (kw kwSemicolon)
ctrs <- braces $ P.sepEndBy (constrDecl sym) (kw delimSemicolon)
lift $ registerInductive ii {_inductiveConstructors = map (^. constructorTag) ctrs}

functionArguments ::
Expand Down Expand Up @@ -252,7 +252,7 @@ typeNamed = do
parseCode ::
(Member InfoTableBuilder r) =>
ParsecS r Code
parseCode = P.sepEndBy command (kw kwSemicolon)
parseCode = P.sepEndBy command (kw delimSemicolon)

command ::
(Member InfoTableBuilder r) =>
Expand Down
11 changes: 7 additions & 4 deletions src/Juvix/Compiler/Concrete/Keywords.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ where
import Juvix.Data.Keyword
import Juvix.Data.Keyword.All
( -- delimiters

delimBraceL,
delimBraceR,
delimJudocBlockEnd,
delimJudocBlockStart,
delimJudocExample,
delimJudocStart,
delimParenL,
delimParenR,
delimSemicolon,
-- keywords
kwAs,
kwAssign,
Expand Down Expand Up @@ -42,7 +46,6 @@ import Juvix.Data.Keyword.All
kwPostfix,
kwPublic,
kwRightArrow,
kwSemicolon,
kwSyntax,
kwTerminating,
kwType,
Expand All @@ -57,7 +60,8 @@ allKeywordStrings = keywordsStrings allKeywords

allKeywords :: [Keyword]
allKeywords =
[ kwAssign,
[ delimSemicolon,
kwAssign,
kwAt,
kwAxiom,
kwCase,
Expand All @@ -75,7 +79,6 @@ allKeywords =
kwPipe,
kwPublic,
kwRightArrow,
kwSemicolon,
kwSyntax,
kwType,
kwUsing,
Expand Down
Loading

0 comments on commit b7b0e10

Please sign in to comment.