-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3011 from quantified-uncertainty/with-location
@location tag
- Loading branch information
Showing
9 changed files
with
171 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@quri/squiggle-lang": patch | ||
"@quri/squiggle-components": patch | ||
--- | ||
|
||
Adds new @location tag and Tag.getLocation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -331,6 +331,7 @@ fn = {|e| e} | |
bar = [x, fn] | ||
@startOpen | ||
@location | ||
s = 4 to 10 | ||
@startClosed | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { Location, LocationRange } from "peggy"; | ||
|
||
import { ImmutableMap } from "../utility/immutableMap.js"; | ||
import { Value, vNumber, vString } from "./index.js"; | ||
import { ValueTags } from "./valueTags.js"; | ||
import { vDict, type VDict } from "./VDict.js"; | ||
|
||
// There are several value tags functions we can't add to ValueTags because Value is dependent on ValueTags, and that would create a circular dependency. | ||
|
||
function locationRangeToValue(locationRange: LocationRange) { | ||
function locationToDict(location: Location): VDict { | ||
return vDict( | ||
ImmutableMap([ | ||
["line", vNumber(location.line)], | ||
["column", vNumber(location.column)], | ||
["offset", vNumber(location.offset)], | ||
]) | ||
); | ||
} | ||
let items: [string, Value][] = [ | ||
["start", locationToDict(locationRange.start)], | ||
["end", locationToDict(locationRange.end)], | ||
]; | ||
if (typeof locationRange.source === "string") { | ||
items = [["source", vString(locationRange.source)], ...items]; | ||
} | ||
return vDict(ImmutableMap(items)); | ||
} | ||
|
||
export const toList = (tags: ValueTags): [string, Value][] => { | ||
const result: [string, Value][] = []; | ||
const { value } = tags; | ||
if (value.name?.value !== undefined) { | ||
result.push(["name", value.name]); | ||
} | ||
if (value.doc?.value !== undefined) { | ||
result.push(["doc", value.doc]); | ||
} | ||
if (value.showAs !== undefined) { | ||
result.push(["showAs", value.showAs]); | ||
} | ||
if (value.numberFormat !== undefined) { | ||
result.push(["numberFormat", value.numberFormat]); | ||
} | ||
if (value.dateFormat !== undefined) { | ||
result.push(["dateFormat", value.dateFormat]); | ||
} | ||
if (value.hidden !== undefined) { | ||
result.push(["hidden", value.hidden]); | ||
} | ||
if (value.notebook !== undefined) { | ||
result.push(["notebook", value.notebook]); | ||
} | ||
const _exportData = tags.exportData(); | ||
if (_exportData !== undefined) { | ||
result.push(["exportData", _exportData]); | ||
} | ||
|
||
if (value.startOpenState !== undefined) { | ||
result.push(["startOpenState", value.startOpenState]); | ||
} | ||
if (value.location !== undefined) { | ||
result.push(["location", locationRangeToValue(value.location)]); | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
export const toMap = (tags: ValueTags): ImmutableMap<string, Value> => { | ||
return ImmutableMap(toList(tags)); | ||
}; | ||
|
||
export const toString = (tags: ValueTags): string => { | ||
return toList(tags) | ||
.map(([key, value]) => `${key}: ${value.toString()}`) | ||
.join(", "); | ||
}; | ||
|
||
export const location = (tags: ValueTags): VDict | undefined => { | ||
const _location = tags.location(); | ||
return _location ? locationRangeToValue(_location) : undefined; | ||
}; |