|
| 1 | +// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. |
| 2 | + |
| 3 | +module internal FSharp.Compiler.LexerStore |
| 4 | + |
| 5 | +open FSharp.Compiler.SyntaxTreeOps |
| 6 | +open FSharp.Compiler.SyntaxTrivia |
| 7 | +open FSharp.Compiler.UnicodeLexing |
| 8 | +open FSharp.Compiler.Text |
| 9 | +open FSharp.Compiler.Text.Position |
| 10 | +open FSharp.Compiler.Text.Range |
| 11 | +open FSharp.Compiler.Xml |
| 12 | + |
| 13 | +//------------------------------------------------------------------------ |
| 14 | +// Lexbuf.BufferLocalStore is used during lexing/parsing of a file for different purposes. |
| 15 | +// All access happens through the functions and modules below. |
| 16 | +//------------------------------------------------------------------------ |
| 17 | + |
| 18 | +let private getStoreData<'T when 'T: not null> (lexbuf: Lexbuf) key (getInitialData: unit -> 'T) = |
| 19 | + let store = lexbuf.BufferLocalStore |
| 20 | + |
| 21 | + match store.TryGetValue key with |
| 22 | + | true, data -> data :?> 'T |
| 23 | + | _ -> |
| 24 | + let data = getInitialData () |
| 25 | + store[key] <- data |
| 26 | + data |
| 27 | + |
| 28 | +let private tryGetStoreData<'T when 'T: not null> (lexbuf: Lexbuf) key = |
| 29 | + let store = lexbuf.BufferLocalStore |
| 30 | + |
| 31 | + match store.TryGetValue key with |
| 32 | + | true, data -> Some(data :?> 'T) |
| 33 | + | _ -> None |
| 34 | + |
| 35 | +let private setStoreData (lexbuf: Lexbuf) key data = lexbuf.BufferLocalStore[key] <- data |
| 36 | + |
| 37 | +//------------------------------------------------------------------------ |
| 38 | +// A SynArgNameGenerator for the current file, used by the parser |
| 39 | +//------------------------------------------------------------------------ |
| 40 | + |
| 41 | +let getSynArgNameGenerator (lexbuf: Lexbuf) = |
| 42 | + getStoreData lexbuf "SynArgNameGenerator" SynArgNameGenerator |
| 43 | + |
| 44 | +//------------------------------------------------------------------------ |
| 45 | +// A XmlDocCollector, used to hold the current accumulated Xml doc lines, and related access functions |
| 46 | +//------------------------------------------------------------------------ |
| 47 | + |
| 48 | +[<RequireQualifiedAccess>] |
| 49 | +module XmlDocStore = |
| 50 | + let private xmlDocKey = "XmlDoc" |
| 51 | + |
| 52 | + let private getCollector (lexbuf: Lexbuf) = |
| 53 | + getStoreData lexbuf xmlDocKey XmlDocCollector |
| 54 | + |
| 55 | + /// Called from the lexer to save a single line of XML doc comment. |
| 56 | + let SaveXmlDocLine (lexbuf: Lexbuf, lineText, range: range) = |
| 57 | + let collector = getCollector lexbuf |
| 58 | + collector.AddXmlDocLine(lineText, range) |
| 59 | + |
| 60 | + let AddGrabPoint (lexbuf: Lexbuf) = |
| 61 | + let collector = getCollector lexbuf |
| 62 | + let startPos = lexbuf.StartPos |
| 63 | + collector.AddGrabPoint(mkPos startPos.Line startPos.Column) |
| 64 | + |
| 65 | + /// Allowed cases when there are comments after XmlDoc |
| 66 | + /// |
| 67 | + /// /// X xmlDoc |
| 68 | + /// // comment |
| 69 | + /// //// comment |
| 70 | + /// (* multiline comment *) |
| 71 | + /// let x = ... // X xmlDoc |
| 72 | + /// |
| 73 | + /// Remember the first position when a comment (//, (* *), ////) is encountered after the XmlDoc block |
| 74 | + /// then add a grab point if a new XmlDoc block follows the comments |
| 75 | + let AddGrabPointDelayed (lexbuf: Lexbuf) = |
| 76 | + let collector = getCollector lexbuf |
| 77 | + let startPos = lexbuf.StartPos |
| 78 | + collector.AddGrabPointDelayed(mkPos startPos.Line startPos.Column) |
| 79 | + |
| 80 | + /// Called from the parser each time we parse a construct that marks the end of an XML doc comment range, |
| 81 | + /// e.g. a 'type' declaration. The markerRange is the range of the keyword that delimits the construct. |
| 82 | + let GrabXmlDocBeforeMarker (lexbuf: Lexbuf, markerRange: range) = |
| 83 | + match tryGetStoreData lexbuf xmlDocKey with |
| 84 | + | Some collector -> PreXmlDoc.CreateFromGrabPoint(collector, markerRange.Start) |
| 85 | + | _ -> PreXmlDoc.Empty |
| 86 | + |
| 87 | + let ReportInvalidXmlDocPositions (lexbuf: Lexbuf) = |
| 88 | + let collector = getCollector lexbuf |
| 89 | + collector.CheckInvalidXmlDocPositions() |
| 90 | + |
| 91 | +//------------------------------------------------------------------------ |
| 92 | +// Storage to hold the current accumulated ConditionalDirectiveTrivia, and related types and access functions |
| 93 | +//------------------------------------------------------------------------ |
| 94 | + |
| 95 | +type LexerIfdefExpression = |
| 96 | + | IfdefAnd of LexerIfdefExpression * LexerIfdefExpression |
| 97 | + | IfdefOr of LexerIfdefExpression * LexerIfdefExpression |
| 98 | + | IfdefNot of LexerIfdefExpression |
| 99 | + | IfdefId of string |
| 100 | + |
| 101 | +let rec LexerIfdefEval (lookup: string -> bool) = |
| 102 | + function |
| 103 | + | IfdefAnd(l, r) -> (LexerIfdefEval lookup l) && (LexerIfdefEval lookup r) |
| 104 | + | IfdefOr(l, r) -> (LexerIfdefEval lookup l) || (LexerIfdefEval lookup r) |
| 105 | + | IfdefNot e -> not (LexerIfdefEval lookup e) |
| 106 | + | IfdefId id -> lookup id |
| 107 | + |
| 108 | +[<RequireQualifiedAccess>] |
| 109 | +module IfdefStore = |
| 110 | + let private getStore (lexbuf: Lexbuf) = |
| 111 | + getStoreData lexbuf "Ifdef" ResizeArray<ConditionalDirectiveTrivia> |
| 112 | + |
| 113 | + let private mkRangeWithoutLeadingWhitespace (lexed: string) (m: range) : range = |
| 114 | + let startColumn = lexed.Length - lexed.TrimStart().Length |
| 115 | + mkFileIndexRange m.FileIndex (mkPos m.StartLine startColumn) m.End |
| 116 | + |
| 117 | + let SaveIfHash (lexbuf: Lexbuf, lexed: string, expr: LexerIfdefExpression, range: range) = |
| 118 | + let store = getStore lexbuf |
| 119 | + |
| 120 | + let expr = |
| 121 | + let rec visit (expr: LexerIfdefExpression) : IfDirectiveExpression = |
| 122 | + match expr with |
| 123 | + | LexerIfdefExpression.IfdefAnd(l, r) -> IfDirectiveExpression.And(visit l, visit r) |
| 124 | + | LexerIfdefExpression.IfdefOr(l, r) -> IfDirectiveExpression.Or(visit l, visit r) |
| 125 | + | LexerIfdefExpression.IfdefNot e -> IfDirectiveExpression.Not(visit e) |
| 126 | + | LexerIfdefExpression.IfdefId id -> IfDirectiveExpression.Ident id |
| 127 | + |
| 128 | + visit expr |
| 129 | + |
| 130 | + let m = mkRangeWithoutLeadingWhitespace lexed range |
| 131 | + |
| 132 | + store.Add(ConditionalDirectiveTrivia.If(expr, m)) |
| 133 | + |
| 134 | + let SaveElseHash (lexbuf: Lexbuf, lexed: string, range: range) = |
| 135 | + let store = getStore lexbuf |
| 136 | + let m = mkRangeWithoutLeadingWhitespace lexed range |
| 137 | + store.Add(ConditionalDirectiveTrivia.Else(m)) |
| 138 | + |
| 139 | + let SaveEndIfHash (lexbuf: Lexbuf, lexed: string, range: range) = |
| 140 | + let store = getStore lexbuf |
| 141 | + let m = mkRangeWithoutLeadingWhitespace lexed range |
| 142 | + store.Add(ConditionalDirectiveTrivia.EndIf(m)) |
| 143 | + |
| 144 | + let GetTrivia (lexbuf: Lexbuf) : ConditionalDirectiveTrivia list = |
| 145 | + let store = getStore lexbuf |
| 146 | + Seq.toList store |
| 147 | + |
| 148 | +//------------------------------------------------------------------------ |
| 149 | +// Storage to hold the current accumulated CommentTrivia, and related access functions |
| 150 | +//------------------------------------------------------------------------ |
| 151 | + |
| 152 | +[<RequireQualifiedAccess>] |
| 153 | +module CommentStore = |
| 154 | + let private getStore (lexbuf: Lexbuf) = |
| 155 | + getStoreData lexbuf "Comments" ResizeArray<CommentTrivia> |
| 156 | + |
| 157 | + let SaveSingleLineComment (lexbuf: Lexbuf, startRange: range, endRange: range) = |
| 158 | + let store = getStore lexbuf |
| 159 | + let m = unionRanges startRange endRange |
| 160 | + store.Add(CommentTrivia.LineComment(m)) |
| 161 | + |
| 162 | + let SaveBlockComment (lexbuf: Lexbuf, startRange: range, endRange: range) = |
| 163 | + let store = getStore lexbuf |
| 164 | + let m = unionRanges startRange endRange |
| 165 | + store.Add(CommentTrivia.BlockComment(m)) |
| 166 | + |
| 167 | + let GetComments (lexbuf: Lexbuf) : CommentTrivia list = |
| 168 | + let store = getStore lexbuf |
| 169 | + Seq.toList store |
0 commit comments