8000 Go Scanner by Eric-Guo · Pull Request #28 · rubychan/coderay · GitHub
[go: up one dir, main page]

Skip to content

Go Scanner #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Go Scanner #28

wants to merge 1 commit into from

Conversation

Eric-Guo
Copy link
Contributor
@Eric-Guo Eric-Guo commented Jul 8, 2012

Draft version, just copy from c and add keywords based on http://golang.org/ref/spec

(Edit @korny: Fixed the title)

Draft version, copy from c
@ghost ghost assigned korny Jul 23, 2012
@korny
Copy link
Member
korny commented Jul 23, 2012

Thanks!

We need some example code to test it. Can you point me to a good source for Go example code, especially using stranger syntax?

@nathany
Copy link
Contributor
nathany commented Jul 27, 2012

Maybe we could slurp up part of the standard library? Or are you looking for a one file example that essentially uses every construct available?

Fyi, the spec is here: http://golang.org/ref/spec

@korny
Copy link
Member
korny commented Jul 27, 2012

are you looking for a one file example that essentially uses every construct available?

That would be awesome.

@Eric-Guo
Copy link
Contributor Author

How about this one, just a part of Go tutorial exercise, a web crawler demo

On Sat, Jul 28, 2012 at 4:10 AM, Kornelius Kalnbach <
reply@reply.github.com

wrote:

are you looking for a one file example that essentially uses every
construct available?

That would be awesome.


Reply to this email directly or view it on GitHub:
#28 (comment)

Eric Guo
SDSS MES Business Systems Analyst

Tel :+86-21-60905910
Mobile: +86-13901862171
www.sandisk.com

@nathany
Copy link
Contributor
nathany commented Jul 28, 2012

@Eric-Guo which one?

@Eric-Guo
Copy link
Contributor Author

This one
http://dl.dropbox.com/u/34650592/2012-07-27-13-24-39-fetcher.go
Sorry for confuse, seem replay from email can not upload the attachment file.

@nathany
Copy link
Contributor
nathany commented Jul 28, 2012

Looks like a good start. It has channels and anonymous functions.

It would be good to have the syntax for Type assertions and Composite literals, since those look a bit funky. Another one I was thinking is when you pass a channel to a function and it only allows incoming or only outgoing channels, just to be sure the little <- arrow appears appropriately in that context.

Fyi, this is the Pygments Lexer for Go, though I don't think it highlights a whole lot.

Once we have support for Go, do you think it would also be worthwhile to add Go templates?

@nathany
Copy link
Contributor
nathany commented Jul 28, 2012

Cgo is also a weird thing, you basically embed C code into a file, though in a comment block? Not sure if there is a nice way to reuse that highlighter.

@korny
Copy link
Member
korny commented Jul 28, 2012

Not sure if there is a nice way to reuse that highlighter.

It's possible, the PHP and ERb do this. I guess we need to keep track of the {…} blocks to find the end of the block then?

regexp.MustCompile(`http://baike.baidu.com/view/[/\w\.-]+`)

The spec says:

Raw string literals are character sequences between back quotes ``. Within the quotes, any character is legal except back quote. The value of a raw string literal is the string composed of the uninterpreted characters between the quotes; in particular, backslashes have no special meaning and the string may contain newlines. Carriage returns inside raw string literals are discarded from the raw string value.

So we need to handle this one differently, like Ruby's single quotes.

@korny
Copy link
Member
korny commented Jul 28, 2012

nathany:

though I don't think it highlights a whole lot.

Is there anything specific you miss in the Pygments version? Usually, their lexers are quite good.

@korny
Copy link
Member
korny commented Jul 28, 2012

@nathany
Copy link
Contributor
nathany commented Jul 28, 2012

GitHub uses Pygments, and when I look at Go code here, such as go-apns, it just looks plain compared to Sublime Text. Of course I tend to use dark themes, so I tried to find a theme that is closer to GitHub to pull out the differences:

https://www.dropbox.com/s/0dzhtm6th7olzhp/sublime-vs-pygments.png

There is definitely more variation here. The receiver (e *Error) is highlighted, the function names are a different color, the short assignment := operator is different than other operators, etc.

Ideally we'll get more themes built into CodeRay, such as Twilight, but that's a separate topic.

@AlanQuatermain
Copy link

If it's of any use, you might take a look at the TextMate bundle I wrote for Go, which parses out a lot of these types for syntax highlighting:

https://github.com/AlanQuatermain/go-tmbundle/blob/master/Syntaxes/Go.tmLanguage

Also: I finished go-apns? Damn, I thought that was just a placeholder with an empty function… Yeah, it's been a while…

@korny
Copy link
Member
korny commented Jul 28, 2012

nathany:

There is definitely more variation here. The receiver (e *Error) is highlighted, the function names are a different color, the short assignment := operator is different than other operators, etc.

Thanks for the screenshots, that's helpful! But I'm not convinced that the Sublime coloring is better.

Why is := different from other operators? Error classes might be okay, because they have very distinctive names. But Sublime highlights more than that: err once in blue (line 99) and once in black (100) — that's not helpful, that's confusing. And what is the reasone behind apns.conn.Read having three different colors?

I wouldn't rate the quality of a highlighter by the number of different colors you get ;) For example, most editors highlight common Ruby methods like print or require in a specal color. CodeRay doesn't, because it's a lie — you don't know whether it is really a call to Kernel#require, or a self-defined method, or just a local variable (ever used p as a local?).

Sorry for the rant. My belief is that a highlighter should only highlight differntly what it can definitely distinguish. Since we're not parsing (just lexing), there's a hard limit for this.

@AlanQuatermain
Copy link

The := operator is a special case: it initializes the left-hand side to the type and value of the right-hand side. It's only usable for initialization of a variable, not assignment. As such, I'd say it's important to call it out with syntax highlighting.

Sent from my iPhone

On 2012-07-28, at 3:47 PM, Kornelius Kalnbach reply@reply.github.com wrote:

Why is := different from other operators?

@korny
Copy link
< 8000 /div>
Member
korny commented Jul 28, 2012

Ah, I see…so := is more like a statement keyword?

@AlanQuatermain
Copy link

Yep, it means 'initialize with', and when using it you don't need to declare the type of the variable. These two are functionally identical:

var myString string = "something"
myString := "something"

Sent from my iPhone

On 2012-07-28, at 4:02 PM, Kornelius Kalnbach reply@reply.github.com wrote:

Ah, I see…so := is more like a statement keyword?


Reply to this email directly or view it on GitHub:
#28 (comment)

@nathany
Copy link
Contributor
nathany commented Jul 28, 2012

I don't know if all the highlighting Sublime is doing is meaningful. ^_^
Just that Github/Pygments feel plain.
Maybe it's just that theme that was weird.

I'm all for having smarter highlighting...
But, taking a look at some code of my own:
https://www.dropbox.com/s/m843apyoj4wactb/mgo.png

  • All the public fields are blue (capitalized) where as private fields (lowercase) would be white.
  • Variables are blue at there initialization site, but white elsewhere.
  • All the function/method calls are yellow, whether standard like panic() or external like Dial().
  • Keywords in the language, like if and defer are orange, as is the := and so is var
  • Not shown in this code, there is a different purple color for the channel <- operator. It is worth distinguishing it from <=.
  • Function definitions are a darker orange color, like main()
  • Some special values like nil and true are a reddish color, as are numeric literals, iota.
  • String literals are green (raw or otherwise).
  • Built in types like string and int are yellow.
  • Comments are grey.

I don't think that's overkill, while still being much more colourful than GitHub/Pygments, which appears to only be highlighting keywords, numeric/string literals, and built in types:

package main

import (
  "fmt"
  "labix.org/v2/mgo"
  "labix.org/v2/mgo/bson"
)

type Person struct {
  Name  string
  Phone string
}

func main() {
  session, err := mgo.Dial("localhost")
  if err != nil {
    panic(err)
  }
  defer session.Close()

  session.SetMode(mgo.Monotonic, true)

  c := session.DB("test").C("people")
  err = c.Insert(
    &Person{"Ale", "+55 53 8116 9639"},
    &Person{"Cla", "+55 53 8402 8510"})
  if err != nil {
    panic(err)
  }

  result := Person{}
  err = c.Find(bson.M{"name": "Ale"}).One(&result)
  if err != nil {
    panic(err)
  }

  fmt.Println("Phone:", result.Phone)
}

@nathany
Copy link
Contributor
nathany commented Jul 28, 2012

Here's one more screenshot to help clarify what it's doing:
https://www.dropbox.com/s/fcx0vrdb4s2lv7z/scope.png

At the bottom you see var z int where var is a keyword (orange), z is the initialization site of the variable (blue), and int is a built-in type. Usage of z below are white. It's jamming this together when initializing y, err above using the :=, in what looks like a consistent way.

Not entirely sure if that's good or bad. Depends on the value of that distinction, I suppose. Maybe it's not that helpful.

It certainly seem like using the sa 8000 me colors to mean different things isn't a great idea (yellow for string and Println, blue for public field usage/definition and for initializing locals). Hmm.

Anyway, my whole point is that we can probably do better than what Pygments currently offers for Go, not that we should copy Sublime Text. In fact, I might need to build a custom color theme for Sublime to match what we come up with.

Nathan.

@ariejan
Copy link
ariejan commented Oct 17, 2012

bump

Is this coming along? Need any help?

@korny
Copy link
Member
korny commented Jun 23, 2013

@nathany:

my whole point is that we can probably do better than what Pygments currently offers for Go

I guess so. But I have not enough knowledge of Go to do that.

So, do we include the scanner as-is in CodeRay 1.1, or do we keep working on it? If so, who implements the improvements?

@nathany
Copy link
Contributor
nathany commented Jun 23, 2013

@korny I think this pull request was a very rough draft based on the C code?

I'm not familiar with writing scanners, so I get pretty lost in the scan_tokens method.

If you want to take a stab at it, the Go language specification is pretty readable, and the grammar is fairly small. The scanner that Go has built in may also be a good reference.

You already have Go sample code, but I will work on some that tries to exercise the whole grammar, with comments to help.

@korny
Copy link
Member
korny commented Jun 23, 2013

I made some updates/fixes. It's presentable now (I think), but not great (as you said).

It would be awesome (and quite CodeRay style) to make a new scanner from scratch after the official scanner. But I think it's a lot of work, turing the char-by-char lexer into a readable regexp-based scanner. I don't have the time to do it myself, and I'd rather work on the Objective-C scanner (planned for 1.2).

So, let's vote. Merge the current version from go-scanner into master, or wait for somebody to write a better one?

@korny
Copy link
Member
korny commented Jun 23, 2013

You already have Go sample code, but I will work on some that tries to exercise the whole grammar, with comments to help.

That would be great in any case.

@nathany
Copy link
Contributor
nathany commented Jun 23, 2013

@korny Do you want all the sample code to be free of errors, at least that it compiles? I just fixed the apns.go sample.

Oh, and I see you added some *.raydebug files to that repo. Do you just copy them across after running rake test:scanner:go with no expected files?

@korny
Copy link
Member
korny commented Jun 23, 2013

Do you just copy them across after running rake test:scanner:go with no expected files?

I added a nicer API, ruby test/scanners/suite.rb go.apns!, for replacing an existing expectation dump.

This was some old pre-Go 1.0 code with lots of errors. Was that intentional?

No ^^ I don't even have a compiler here.

@nathany
Copy link
Contributor
nathany commented Jun 23, 2013

Rouge has an interesting way of testing the highlighter, though this isn't exactly valid code. What do you think?

@nathany nathany mentioned this pull request Jun 23, 2013
5 tasks
@korny
Copy link
Member
korny commented Jun 23, 2013

@nathany:

Rouge has an interesting way of testing the highlighter, though this isn't exactly valid code. What do you think?

Interesting idea. I don't think it's a replacement to real example code, but a very nice addition to the usual examples.

@korny
Copy link
Member
korny commented Jul 13, 2013

superseded by #147, @nathany's any my tweaked version.

@korny korny closed this Jul 13, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0