Commit Graph

133 Commits

Author SHA1 Message Date
Andrew Hickman
3a3e09c285 Fix build on wasm with serialization feature`
serde` only implements `Serialize` for `OsString` on
unix and windows.
2020-09-04 12:17:23 +01:00
Johann150
593ed5372b
Return errors instead of panicking (#275)
Co-authored-by: Markus Westerlind <marwes91@gmail.com>
Co-authored-by: Brendan Zabarauskas <bjzaba@yahoo.com.au>
Co-authored-by: Markus Westerlind <marwes91@gmail.com>
2020-08-31 11:00:36 +10:00
Brendan Zabarauskas
bc9d399134 Bump cargo version to v0.9.5 2020-06-24 13:34:09 +10:00
Joshua Nelson
54c762a25b Allow using codespan without codespan-reporting
This copies the implementation of `line_starts` to `codespan`.
2020-06-21 22:53:51 -04:00
Brendan Zabarauskas
ec2135ca9c Bump cargo version to v0.9.4 2020-05-18 16:07:30 +10:00
Brendan Zabarauskas
17ab90736e Bump cargo version to v0.9.3 2020-04-29 13:27:27 +10:00
Brendan Zabarauskas
0facec4c8d Bump cargo version to v0.9.2 2020-03-29 13:12:19 +11:00
Brendan Zabarauskas
2da3088d1f Bump cargo version to v0.9.1 2020-03-23 10:32:51 +11:00
Brendan Zabarauskas
239269dadd Implement some conversions for indexes and spans 2020-03-20 11:27:59 +11:00
Brendan Zabarauskas
9e9759549f Bump cargo version to v0.9.0 2020-03-15 16:22:46 +11:00
Brendan Zabarauskas
c2e0744ae9 Add changelogs to crates
Better late than never!
2020-03-10 12:48:23 +11:00
Brendan Zabarauskas
cda55793d2 Remove publish script
I never actually used this! Might as well remove it.
2020-03-10 12:48:11 +11:00
Brendan Zabarauskas
d7c871fa80 Rename Files::Origin to Files::Name and improve docs 2020-03-10 09:00:03 +11:00
Brendan Zabarauskas
0b9627187a Make codespan-reporting more prominent in README
Since we are trying to now guide people to use `codespan-reporting` directly without relying on codespan, it makes sense to make it more prominent in the README.
2020-03-09 16:24:48 +11:00
Brendan Zabarauskas
3d7028e997 Update cargo dependencies 2020-03-09 15:19:08 +11:00
Brendan Zabarauskas
2b867f6bc1 Remove the Line struct 2020-03-09 15:12:57 +11:00
Brendan Zabarauskas
7d0b749cb3 Remove source field from Line struct
This makes the rendering code a little more clunky, but I think it makes the API of the `Files` trait a bit nicer for implementors of file databases.
2020-03-09 12:36:09 +11:00
Brendan Zabarauskas
4d0bbb6aa7 Make line_starts function public
This can make it easier to implement new `Files` implementations.
2020-03-06 17:28:09 +11:00
Brendan Zabarauskas
13d3a1754c Remove rustfmt config 2020-03-03 16:37:21 +11:00
Brendan Zabarauskas
b48c8c01b7 Allow references to be returned from lines 2020-02-27 10:46:30 +11:00
Brendan Zabarauskas
0861df7150 Move file-related items into a submodule 2020-02-27 09:54:23 +11:00
Brendan Zabarauskas
77160d0821 Add reporting feature to codespan crate 2020-02-27 09:21:00 +11:00
Brendan Zabarauskas
c16dfab918 Add SimpleFile and SimpleFiles structs
This adds the `SimpleFile` and `SimpleFiles` structures to the `codespan_reporting` crate to get people started with their programming languages. It also inverts the dependency between `codespan` and `codespan_reporting`. That way users of `codespan_reporting` no longer have to depend on `codespan` if they don’t want to.
2020-02-26 21:42:19 +11:00
Brendan Zabarauskas
1a68b22d49 Create Files trait 2020-02-24 22:50:33 +11:00
Brendan Zabarauskas
01ec3e44fc
Merge pull request #166 from brendanzab/reduce-reallocations
Avoid string allocations when printing source snippets
2020-02-24 22:46:51 +11:00
Brendan Zabarauskas
f1ff1dc96a Release v0.8.0
Closes #163.
2020-02-24 14:56:11 +11:00
Brendan Zabarauskas
b7eb601982 Don’t allocate when measuring line numbers 2020-02-23 14:53:27 +11:00
Brendan Zabarauskas
5d559fcdeb Remove some superfluous type bounds 2020-02-20 14:51:42 +11:00
Brendan Zabarauskas
5f76474978
Merge pull request #151 from jyn514/osstr
String -> OsString
2020-01-15 15:08:52 +11:00
Joshua Nelson
ff2b7d5aec
String -> OsString
Allows storing arbitrary filenames in `File`

Closes https://github.com/brendanzab/codespan/issues/150
2020-01-14 22:19:17 -05:00
Joshua Nelson
aaf491e2ec
Fix formatting #2
Co-Authored-By: Brendan Zabarauskas <bjzaba@yahoo.com.au>
2020-01-14 07:46:03 -05:00
Joshua Nelson
b24de85af3
Fix formatting
Co-Authored-By: Brendan Zabarauskas <bjzaba@yahoo.com.au>
2020-01-14 07:45:43 -05:00
Joshua Nelson
23c276579a
add disjoint() and merge() 2020-01-14 00:05:32 -05:00
Brendan Zabarauskas
1d6f3a6131 Bump cargo version to 0.7.0 2020-01-06 11:14:46 +11:00
Brendan Zabarauskas
1aec895d22 Remove memory_usage feature
The `heapsize` crate is now unmaintained (since servo/heapsize#105).

Closes #146.
2020-01-06 11:14:46 +11:00
Brendan Zabarauskas
c3af8bf382 Improve type inference for Files::{add, update}
This removes the implicit conversions for the `Files::{add, update}` methods now that we parameterise `Files` by the source type (see #144).
2020-01-06 11:06:54 +11:00
Ryan Cumming
6000d4367b Merge branch 'master' into make-source-type-generic 2020-01-05 14:20:08 +11:00
Brendan Zabarauskas
c760bff07d
Merge pull request #144 from etaoins/make-file-id-nonzero
Make `FileId` non-zero
2020-01-05 12:50:36 +11:00
Brendan Zabarauskas
461beacb25
Merge pull request #145 from etaoins/have-span-take-self-by-value
Have `Span` take self by value
2020-01-05 12:48:55 +11:00
Ryan Cumming
fe98215490 Make source type generic
**THIS IS A BREAKING CHANGE**

When porting Arret from `codespan` 0.3 (see etaoins/arret#160) one of
the missing features was being able use non-`String` types for storing
source.

Arret has two potential uses for this:

1. When loading native libraries exposing Arret functions (known as RFI
   libraries) their Arret type is specified as a per-function
   `&'static str`. An example can be seen here:

   dfc41bed1c/stdlib/rust/list.rs (L8)

   With `codespan` 0.3 we used `Cow<'static str>` to avoid allocating a
   new `String` for every RFI function by pointing directly inside the
   loaded library's binary.

2. Arret is multithreaded but needs to maintain a global `Files`
   instance to allocate `FileId`s. It uses a read-write lock for thread
   safety and is sensitive to contention on that lock in some
   circumstances.

   Because we need a `FileId` in our AST we can't parse the source to
   AST until we've added it to `Files`. However, because `Files` takes
   ownership of the source we need to hold a read lock on `Files` to use
   `files.source()`. The effectively serialises loading source files
   across all threads.

   Previously we used `Arc<FileMap>` to drop the lock on `CodeMap` and
   then parse the `FileMap` locklessly. The API doesn't allow this
   anymore but we could use `Source = Arc<str>` to similar effect.

Unfortunately the above two uses are mutually exclusive using standard
library types. However, either one is an improvement over `String` and a
crate type like `supercow` could potentially satisfy both uses.

This is a breaking change due to adding a new generic parameter to
`Files`. I initially attempted to use a default of `String` but this
doesn't work for `Files::new` due to rust-lang/rust#27336. See
rust-lang/wg-allocators#1 for an analogous case to this.
2020-01-05 12:01:57 +11:00
Ryan Cumming
11f3c182c7 Implement heapsize::HeapSizeOf for FileId
We can't derive this automatically as `HeapSizeOf` doesn't know about
`NonZeroU32`
2020-01-05 11:11:49 +11:00
Ryan Cumming
3412cb625b Have Span take self by value
This was caught by Clippy. `Span` is an 8 byte copy type so it makes
sense to take it by value instead of reference. This is consistent with
what `merge` was already doing.
2020-01-05 10:28:31 +11:00
Ryan Cumming
edd0e94994 Make FileId non-zero
When porting from `codespan` 0.3 Arret I needed to track the `FileId`
separately from the `Span` now that the file isn't implied in the span
itself. Having a simple struct like this works for 90% of the cases:

```rust
struct Span {
  file_id: codespan::FileId,
  codespan_span: codespan::Span,
}
```

However, this requires that every time we parse any source we require a
`FileId` which requires a `codespan::Files` to create. This is overkill
in a couple of cases:

1. Unit tests that just need to parse simple Arret strings as a
   shorthand for building our AST

2. Syntax highlighting in the REPL

The compromise in etaoins/arret#160 is to use `Option<FileId>`. I'm not
100% happy with that as the ultimate solution but it was the easiest
path to take during porting. However, this makes the size of the above
struct 13 bytes which will typically be rounded up to 16 bytes for
alignment and padding.

We can use `std::num::NonZeroU32` instead and offset all of our indexes
by 1. This allows the Rust compiler to use 0 to represent the `None`
case and bring the size down to 12 bytes. Because the struct only needs
an alignment of 4 bytes this is more likely to stay as 12 bytes in most
contexts.

A nicer solution would be if Rust had e.g. `NonMaxU32` but it appears
the Rust team instead wants to use const generics so arbitrary niche
values can be specified.
2020-01-05 10:16:35 +11:00
Ryan Cumming
f521ca7a4d Make Span::initial() a const fn
In Arret nearly every intermediate data structure contains a `Span` so
our generated code can be mapped back to source location. However,
they're rarely used until we need to report a diagnostic or generate
debugging information.

This led to a common pattern in unit tests where we use a dummy span to
test intermediate transformations where we aren't starting from actual
Arret source. This used to be a `const` called `EMPTY_SPAN` which can be
used to build other `const` data structures used for testing.

In etaoins/arret#160 we needed to switch to an `empty_span()` function
because there is no longer a `const` constructor for `Span`.

We can't make `Span::new` `const` yet because const functions are not
powerful enough. However, `Span::initial` is perfect for this use case.
2020-01-05 09:55:23 +11:00
David Tolnay
872b46bf1f
Fix gutter misalignments 2019-12-28 22:24:56 -05:00
Brendan Zabarauskas
30be182eca Bump version to v0.6.0 2019-12-18 12:03:16 +11:00
Brendan Zabarauskas
ed99d65cdc Update structopt and unicode-segmentation 2019-12-18 10:40:47 +11:00
Markus Westerlind
ef4b7ea583 Implement Default for Files 2019-10-04 21:43:00 +02:00
Brendan Zabarauskas
8313476549 Bump version to v0.5.0 2019-10-02 11:14:22 +10:00
Brendan Zabarauskas
b7e867bcbd Bump version to v0.4.1 2019-08-25 14:38:35 +10:00