From 27b8fefad6c9c885aa9087c32d243e5087b61c13 Mon Sep 17 00:00:00 2001 From: Stachu Korick Date: Tue, 30 Apr 2024 12:43:04 -1000 Subject: [PATCH] SourceRange -> Range; fix some tests --- backend/src/BuiltinExecution/Libs/Parser.fs | 4 +- backend/src/LibParser/Canvas.fs | 1 - .../LibParser/WrittenTypesToProgramTypes.fs | 1 - backend/src/LocalExec/LoadPackagesFromDisk.fs | 2 +- .../lsp-server/semanticTokens.dark | 16 +- .../languageTools/parser/identifiers.dark | 10 +- .../languageTools/parser/typeDeclaration.dark | 4 +- .../languageTools/semanticTokens.dark | 14 +- .../darklang/languageTools/writtenTypes.dark | 281 ++++++++---------- .../writtenTypesToProgramTypes.dark | 16 +- 10 files changed, 153 insertions(+), 196 deletions(-) diff --git a/backend/src/BuiltinExecution/Libs/Parser.fs b/backend/src/BuiltinExecution/Libs/Parser.fs index 5d3fab332f..e81b378f03 100644 --- a/backend/src/BuiltinExecution/Libs/Parser.fs +++ b/backend/src/BuiltinExecution/Libs/Parser.fs @@ -59,7 +59,7 @@ let fns : List = let startPos = cursor.Current.StartPosition let endPos = cursor.Current.EndPosition - let sourceRange = + let range = let fields = [ "start", mapPoint startPos; "end_", mapPoint endPos ] DRecord(rangeTypeName, rangeTypeName, [], Map fields) @@ -91,7 +91,7 @@ let fns : List = [ ("fieldName", fieldName) ("typ", DString cursor.Current.Kind) ("text", DString sourceText) - ("range", sourceRange) + ("range", range) ("children", DList(VT.customType parsedNodeTypeName [], children)) ] DRecord(parsedNodeTypeName, parsedNodeTypeName, [], Map fields) diff --git a/backend/src/LibParser/Canvas.fs b/backend/src/LibParser/Canvas.fs index f3804087b1..9b52b094ff 100644 --- a/backend/src/LibParser/Canvas.fs +++ b/backend/src/LibParser/Canvas.fs @@ -315,7 +315,6 @@ let parse None // whole file let moduleWT = parseDecls owner canvasName decls - debuG "Parsed module" moduleWT // Initial pass, so we can re-parse with all names in context let! result = toPT builtins pm onMissing moduleWT diff --git a/backend/src/LibParser/WrittenTypesToProgramTypes.fs b/backend/src/LibParser/WrittenTypesToProgramTypes.fs index 17022ab602..ccb518bb2a 100644 --- a/backend/src/LibParser/WrittenTypesToProgramTypes.fs +++ b/backend/src/LibParser/WrittenTypesToProgramTypes.fs @@ -180,7 +180,6 @@ module Expr = | WT.EBool(id, b) -> return PT.EBool(id, b) | WT.EUnit id -> return PT.EUnit id | WT.EVariable(id, var) -> - if var = "format" then debuG "format" (currentModule) // This could be a UserConstant let! constant = NR.resolveConstantName diff --git a/backend/src/LocalExec/LoadPackagesFromDisk.fs b/backend/src/LocalExec/LoadPackagesFromDisk.fs index cff35f99f1..7e7e1d2e95 100644 --- a/backend/src/LocalExec/LoadPackagesFromDisk.fs +++ b/backend/src/LocalExec/LoadPackagesFromDisk.fs @@ -29,7 +29,7 @@ let load (builtins : RT.Builtins) : Ply = filesWithContents // TODO: parallelize |> Ply.List.mapSequentially (fun (path, contents) -> - //debuG "parsing" path + debuG "parsing" path LibParser.Parser.parsePackageFile builtins RT.PackageManager.empty diff --git a/packages/darklang/languageTools/lsp-server/semanticTokens.dark b/packages/darklang/languageTools/lsp-server/semanticTokens.dark index 995f436c9a..a1595d373e 100644 --- a/packages/darklang/languageTools/lsp-server/semanticTokens.dark +++ b/packages/darklang/languageTools/lsp-server/semanticTokens.dark @@ -61,8 +61,8 @@ module Darklang = : List> = tokens |> Stdlib.List.sortByComparator_v0 (fun a b -> - let aStart = a.sourceRange.start - let bStart = b.sourceRange.start + let aStart = a.range.start + let bStart = b.range.start if Stdlib.Int64.lessThan aStart.row bStart.row then -1L @@ -81,14 +81,13 @@ module Darklang = let (tokensSoFar, startOfLastToken) = acc let (deltaLine, deltaStart) = - match token.sourceRange.start.row - startOfLastToken.row with - | 0L -> - (0UL, token.sourceRange.start.column - startOfLastToken.column) + match token.range.start.row - startOfLastToken.row with + | 0L -> (0UL, token.range.start.column - startOfLastToken.column) | lineDiff -> let lineDiff = lineDiff |> Stdlib.UInt64.fromInt64 |> Builtin.unwrap - (lineDiff, token.sourceRange.start.column) + (lineDiff, token.range.start.column) let newToken = LanguageServerProtocol.SemanticTokens.RelativeSemanticToken.RelativeSemanticToken // @@ -96,13 +95,12 @@ module Darklang = deltaStart = deltaStart |> Stdlib.UInt64.fromInt64 |> Builtin.unwrap length = - (token.sourceRange.end_.column - - token.sourceRange.start.column) + (token.range.end_.column - token.range.start.column) |> Stdlib.UInt64.fromInt64 |> Builtin.unwrap tokenType = token.tokenType } - (Stdlib.List.push tokensSoFar newToken, token.sourceRange.start))) + (Stdlib.List.push tokensSoFar newToken, token.range.start))) |> Stdlib.Tuple2.first |> Stdlib.List.reverse diff --git a/packages/darklang/languageTools/parser/identifiers.dark b/packages/darklang/languageTools/parser/identifiers.dark index 135e2d9bb2..dba2b40ba9 100644 --- a/packages/darklang/languageTools/parser/identifiers.dark +++ b/packages/darklang/languageTools/parser/identifiers.dark @@ -4,11 +4,9 @@ module Darklang = module Identifiers = let extractModuleIdentifiersHelper (modulesSoFarInReverse: - List) + List) (nodes: List) - : (List * - ParsedNode) - = + : (List * ParsedNode) = match nodes with | modulePart :: symbolPart :: otherParts -> if modulePart.typ != "module_identifier" then @@ -38,9 +36,7 @@ module Darklang = /// ensuring that we take note of the `.` source ranges after each module name let extractModuleIdentifiers (nodes: List) - : (List * - ParsedNode) - = + : (List * ParsedNode) = let (modulesInReverse, lastNode) = extractModuleIdentifiersHelper [] nodes (Stdlib.List.reverse modulesInReverse, lastNode) diff --git a/packages/darklang/languageTools/parser/typeDeclaration.dark b/packages/darklang/languageTools/parser/typeDeclaration.dark index 9dc6183550..c2fdd88fc9 100644 --- a/packages/darklang/languageTools/parser/typeDeclaration.dark +++ b/packages/darklang/languageTools/parser/typeDeclaration.dark @@ -83,10 +83,10 @@ module Darklang = | [ fieldNode; separator ] -> (parseRecordField fieldNode, - Stdlib.Option.Option.Some separator.sourceRange) + Stdlib.Option.Option.Some separator.range) | _ -> (createUnparseableError chunk, - Stdlib.Option.Option.Some separator.sourceRange)) + Stdlib.Option.Option.Some separator.range)) | None -> (Stdlib.Result.Result.Ok [], Stdlib.Option.Option.None) diff --git a/packages/darklang/languageTools/semanticTokens.dark b/packages/darklang/languageTools/semanticTokens.dark index c969536050..aed87cf384 100644 --- a/packages/darklang/languageTools/semanticTokens.dark +++ b/packages/darklang/languageTools/semanticTokens.dark @@ -2,7 +2,7 @@ module Darklang = module LanguageTools = module SemanticTokens = // - type SourceRange = Parser.Range + type Range = Parser.Range // type TokenType = @@ -46,12 +46,10 @@ module Darklang = /// /// These tokens mean little without reference to a document where the /// 'ranges' live within. - type SemanticToken = - { sourceRange: SourceRange - tokenType: TokenType } + type SemanticToken = { range: Range; tokenType: TokenType } - let makeToken (s: SourceRange) (t: TokenType) : SemanticToken = - SemanticToken { sourceRange = s; tokenType = t } + let makeToken (s: Range) (t: TokenType) : SemanticToken = + SemanticToken { range = s; tokenType = t } module ModuleIdentifier = @@ -406,13 +404,13 @@ module Darklang = fields |> Stdlib.List.map (fun (symbol, fieldName, value) -> // TODO: use tuple destructuring, once nested tuple destructuring in a lambda is fixed - // i.e. fun (symbol, (sourceRange, _), value) -> ... instead of the below code + // i.e. fun (symbol, (range, _), value) -> ... instead of the below code let (range, _) = fieldName [ // name [ makeToken range TokenType.Property ] // = - [ makeToken symbol.sourceRange TokenType.Symbol ] + [ makeToken symbol.range TokenType.Symbol ] // "Alice" Expr.tokenize value ]) |> Stdlib.List.flatten diff --git a/packages/darklang/languageTools/writtenTypes.dark b/packages/darklang/languageTools/writtenTypes.dark index 69aae508e2..fd4d205f38 100644 --- a/packages/darklang/languageTools/writtenTypes.dark +++ b/packages/darklang/languageTools/writtenTypes.dark @@ -2,82 +2,82 @@ module Darklang = module LanguageTools = module WrittenTypes = // - type SourceRange = Parser.Range + type Range = Parser.Range // // Identifiers - type ModuleIdentifier = { range: SourceRange; name: String } + type ModuleIdentifier = { range: Range; name: String } - type TypeIdentifier = { range: SourceRange; name: String } + type TypeIdentifier = { range: Range; name: String } type QualifiedTypeIdentifier = { - range: SourceRange - /// the SourceRange corresponds to the `.` after the module name - modules: List + range: Range + /// the Range corresponds to the `.` after the module name + modules: List typ: TypeIdentifier } - type VariableIdentifier = { range: SourceRange; name: String } + type VariableIdentifier = { range: Range; name: String } - type FnIdentifier = { range: SourceRange; name: String } + type FnIdentifier = { range: Range; name: String } type QualifiedFnIdentifier = { - range: SourceRange - /// the sourceRange corresponds to the `.` after the module name - modules: List + range: Range + /// the range corresponds to the `.` after the module name + modules: List fn: FnIdentifier } - type Name = Unresolved of SourceRange * List + type Name = Unresolved of Range * List // Types module TypeReference = - // The first sourceRange corresponds to the whole type + // The first range corresponds to the whole type type Builtin = - | TUnit of SourceRange - | TBool of SourceRange - | TInt8 of SourceRange - | TUInt8 of SourceRange - | TInt16 of SourceRange - | TUInt16 of SourceRange - | TInt32 of SourceRange - | TUInt32 of SourceRange - | TInt64 of SourceRange - | TUInt64 of SourceRange - | TInt128 of SourceRange - | TUInt128 of SourceRange - | TFloat of SourceRange - | TChar of SourceRange - | TString of SourceRange + | TUnit of Range + | TBool of Range + | TInt8 of Range + | TUInt8 of Range + | TInt16 of Range + | TUInt16 of Range + | TInt32 of Range + | TUInt32 of Range + | TInt64 of Range + | TUInt64 of Range + | TInt128 of Range + | TUInt128 of Range + | TFloat of Range + | TChar of Range + | TString of Range | TList of - SourceRange * - keywordList: SourceRange * - openBracket: SourceRange * + Range * + keywordList: Range * + openBracket: Range * typ: TypeReference.TypeReference * - closeBracket: SourceRange + closeBracket: Range | TDict of - SourceRange * - keywordDict: SourceRange * - openBrace: SourceRange * + Range * + keywordDict: Range * + openBrace: Range * typ: TypeReference.TypeReference * - closeBrace: SourceRange + closeBrace: Range | TTuple of - SourceRange * + Range * first: TypeReference * - symbolAsterisk: SourceRange * + symbolAsterisk: Range * second: TypeReference * - rest: List * TypeReference> * - openParen: SourceRange * - closeParen: SourceRange + rest: List * TypeReference> * + openParen: Range * + closeParen: Range - | TDateTime of SourceRange - | TUuid of SourceRange + | TDateTime of Range + | TUuid of Range type TypeReference = @@ -88,42 +88,42 @@ module Darklang = module TypeDeclaration = type RecordField = - { range: SourceRange - name: SourceRange * String + { range: Range + name: Range * String typ: TypeReference.TypeReference description: String - symbolColon: SourceRange } + symbolColon: Range } type EnumField = - { range: SourceRange + { range: Range typ: TypeReference.TypeReference - label: Stdlib.Option.Option + label: Stdlib.Option.Option description: String - symbolColon: Stdlib.Option.Option } + symbolColon: Stdlib.Option.Option } type EnumCase = - { range: SourceRange - name: SourceRange * String + { range: Range + name: Range * String fields: List description: String - keywordOf: Stdlib.Option.Option } + keywordOf: Stdlib.Option.Option } type Definition = | Alias of TypeReference.TypeReference - | Record of List> + | Record of List> // TODO: capture the |s that precede the EnumCases | Enum of List type TypeDeclaration = - { range: SourceRange + { range: Range name: TypeIdentifier definition: Definition - keywordType: SourceRange - symbolEquals: SourceRange } + keywordType: Range + symbolEquals: Range } // Expressions - type LetPattern = LPVariable of SourceRange * name: String + type LetPattern = LPVariable of Range * name: String type Infix = | InfixFnCall of InfixFnName @@ -149,148 +149,117 @@ module Darklang = | BinOpOr type Expr = - | EUnit of SourceRange - | EBool of SourceRange * Bool - | EInt8 of - SourceRange * - intPart: (SourceRange * Int8) * - suffixPart: SourceRange - | EUInt8 of - SourceRange * - intPart: (SourceRange * UInt8) * - suffixPart: SourceRange - | EInt16 of - SourceRange * - intPart: (SourceRange * Int16) * - suffixPart: SourceRange - | EUInt16 of - SourceRange * - intPart: (SourceRange * UInt16) * - suffixPart: SourceRange - | EInt32 of - SourceRange * - intPart: (SourceRange * Int32) * - suffixPart: SourceRange - | EUInt32 of - SourceRange * - intPart: (SourceRange * UInt32) * - suffixPart: SourceRange + | EUnit of Range + | EBool of Range * Bool + | EInt8 of Range * intPart: (Range * Int8) * suffixPart: Range + | EUInt8 of Range * intPart: (Range * UInt8) * suffixPart: Range + | EInt16 of Range * intPart: (Range * Int16) * suffixPart: Range + | EUInt16 of Range * intPart: (Range * UInt16) * suffixPart: Range + | EInt32 of Range * intPart: (Range * Int32) * suffixPart: Range + | EUInt32 of Range * intPart: (Range * UInt32) * suffixPart: Range | EInt64 of - SourceRange * - intPart: (SourceRange * Int64) * + Range * + intPart: (Range * Int64) * /// for the L suffix - suffixPart: SourceRange - | EUInt64 of - SourceRange * - intPart: (SourceRange * UInt64) * - suffixPart: SourceRange - | EInt128 of - SourceRange * - intPart: (SourceRange * Int128) * - suffixPart: SourceRange - | EUInt128 of - SourceRange * - intPart: (SourceRange * UInt128) * - suffixPart: SourceRange - | EFloat of SourceRange * Sign * String * String + suffixPart: Range + | EUInt64 of Range * intPart: (Range * UInt64) * suffixPart: Range + | EInt128 of Range * intPart: (Range * Int128) * suffixPart: Range + | EUInt128 of Range * intPart: (Range * UInt128) * suffixPart: Range + | EFloat of Range * Sign * String * String | EString of - SourceRange * - contents: Stdlib.Option.Option * - symbolOpenQuote: SourceRange * - symbolCloseQuote: SourceRange + Range * + contents: Stdlib.Option.Option * + symbolOpenQuote: Range * + symbolCloseQuote: Range | EChar of - SourceRange * - contents: Stdlib.Option.Option * - symbolOpenQuote: SourceRange * - symbolCloseQuote: SourceRange + Range * + contents: Stdlib.Option.Option * + symbolOpenQuote: Range * + symbolCloseQuote: Range | EList of - SourceRange * - contents: List> * - symbolOpenBracket: SourceRange * - symbolCloseBracket: SourceRange + Range * + contents: List> * + symbolOpenBracket: Range * + symbolCloseBracket: Range | EDict of - SourceRange * - contents: List * - keywordDict: SourceRange * - symbolOpenBrace: SourceRange * - symbolCloseBrace: SourceRange + Range * + contents: List * + keywordDict: Range * + symbolOpenBrace: Range * + symbolCloseBrace: Range | ETuple of - SourceRange * + Range * first: Expr * - symbolComma: SourceRange * + symbolComma: Range * second: Expr * - rest: List * Expr> * - symbolOpenParen: SourceRange * - symbolCloseParen: SourceRange + rest: List * Expr> * + symbolOpenParen: Range * + symbolCloseParen: Range | ERecord of - SourceRange * + Range * typeName: Name * - fields: List * - symbolOpenBrace: SourceRange * - symbolCloseBrace: SourceRange + fields: List * + symbolOpenBrace: Range * + symbolCloseBrace: Range | EEnum of - SourceRange * - typeName: (SourceRange * List) * - caseName: (SourceRange * String) * + Range * + typeName: (Range * List) * + caseName: (Range * String) * fields: List * /// between the typeName and the caseName - symbolDot: SourceRange + symbolDot: Range | ELet of - SourceRange * + Range * LetPattern * expr: Expr * body: Expr * - keywordLet: SourceRange * - symbolEquals: SourceRange + keywordLet: Range * + symbolEquals: Range - | EVariable of SourceRange * String + | EVariable of Range * String | EIf of - SourceRange * + Range * cond: Expr * thenExpr: Expr * elseExpr: Stdlib.Option.Option * - keywordIf: SourceRange * - keywordThen: SourceRange * - keywordElse: Stdlib.Option.Option + keywordIf: Range * + keywordThen: Range * + keywordElse: Stdlib.Option.Option - | EInfix of - SourceRange * - op: (SourceRange * Infix) * - left: Expr * - right: Expr + | EInfix of Range * op: (Range * Infix) * left: Expr * right: Expr // TODO: I accidentally got away from how we normally represent // Expressions - switch to this instead. - // | EApply of SourceRange * lhs: Expr * args: List + // | EApply of Range * lhs: Expr * args: List // | EFnName of QualifiedFnIdentifier | EFnCall of - SourceRange * + Range * fnName: QualifiedFnIdentifier * args: List * - symbolLeftParen: SourceRange * - symbolRightParen: SourceRange + symbolLeftParen: Range * + symbolRightParen: Range // Fn declarations module FnDeclaration = - type UnitParameter = { range: SourceRange } + type UnitParameter = { range: Range } type NormalParameter = - { range: SourceRange + { range: Range name: VariableIdentifier typ: TypeReference.TypeReference - symbolLeftParen: SourceRange - symbolColon: SourceRange - symbolRightParen: SourceRange } + symbolLeftParen: Range + symbolColon: Range + symbolRightParen: Range } type Parameter = | Unit of UnitParameter @@ -298,14 +267,14 @@ module Darklang = type FnDeclaration = - { range: SourceRange + { range: Range name: FnIdentifier parameters: List returnType: TypeReference.TypeReference body: Expr - keywordLet: SourceRange - symbolColon: SourceRange - symbolEquals: SourceRange } + keywordLet: Range + symbolColon: Range + symbolEquals: Range } // Cli scripts @@ -316,7 +285,7 @@ module Darklang = type Unparseable = { source: Parser.ParsedNode } type CliScript = - { range: SourceRange + { range: Range typesAndFns: List unparseableStuff: List exprsToEval: List } diff --git a/packages/darklang/languageTools/writtenTypesToProgramTypes.dark b/packages/darklang/languageTools/writtenTypesToProgramTypes.dark index d77a76725f..570cbce135 100644 --- a/packages/darklang/languageTools/writtenTypesToProgramTypes.dark +++ b/packages/darklang/languageTools/writtenTypesToProgramTypes.dark @@ -120,7 +120,7 @@ module Darklang = module EnumField = let toPT - (resolver: NameResolver.NameResolutionSettings) + (onMissing: NameResolver.OnMissing) (f: WrittenTypes.TypeDeclaration.EnumField) : ProgramTypes.TypeDeclaration.EnumField = @@ -130,20 +130,20 @@ module Darklang = | None -> Stdlib.Option.Option.None ProgramTypes.TypeDeclaration.EnumField - { typ = TypeReference.toPT resolver f.typ + { typ = TypeReference.toPT onMissing f.typ label = label description = "" } module EnumCase = let toPT - (resolver: NameResolver.NameResolutionSettings) + (onMissing: NameResolver.OnMissing) (c: WrittenTypes.TypeDeclaration.EnumCase) : ProgramTypes.TypeDeclaration.EnumCase = let name = c.name |> Stdlib.Tuple2.second let fields = - Stdlib.List.map c.fields (fun f -> EnumField.toPT resolver f) + Stdlib.List.map c.fields (fun f -> EnumField.toPT onMissing f) ProgramTypes.TypeDeclaration.EnumCase { name = name @@ -161,8 +161,6 @@ module Darklang = let typ = TypeReference.toPT onMissing typ ProgramTypes.TypeDeclaration.Definition.Alias typ - | Enum _cases -> "TODO" - | Record fields -> let fields = Stdlib.List.map fields (fun (f, _) -> RecordField.toPT onMissing f) @@ -170,7 +168,7 @@ module Darklang = ProgramTypes.TypeDeclaration.Definition.Record fields | Enum cases -> - let cases = Stdlib.List.map cases (fun c -> EnumCase.toPT resolver c) + let cases = Stdlib.List.map cases (fun c -> EnumCase.toPT onMissing c) ProgramTypes.TypeDeclaration.Definition.Enum cases let toPT @@ -313,12 +311,12 @@ module Darklang = let typeName = NameResolver.TypeName.resolve - resolver + onMissing [] (WrittenTypes.Name.Unresolved sr unresolvedTypeName) let caseName = caseName |> Stdlib.Tuple2.second - let fields = Stdlib.List.map fields (fun expr -> toPT resolver expr) + let fields = Stdlib.List.map fields (fun expr -> toPT onMissing expr) ProgramTypes.Expr.EEnum(gid (), typeName, caseName, fields)