-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_basic_test.go
55 lines (44 loc) · 1.03 KB
/
example_basic_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright 2018 Josh Lubawy. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ctext_test
import (
"fmt"
"io"
"log"
"strings"
"github.com/jlubawy/go-ctext"
)
const BasicProgram = `/**
* hello_world.c
*/
#include <stdio.h>
int
main( void )
{
fputs( "Hello world\n", stdout );
return 0;
}
`
func Example_basic() {
s := ctext.NewScanner(strings.NewReader(BasicProgram))
s.Filename = "hello_world.c"
for {
tt := s.Next()
switch tt {
case ctext.ErrorToken:
err := s.Err()
if err == io.EOF {
return
}
log.Fatal(err)
case ctext.CommentToken:
fmt.Printf("<comment> %s: %q\n", s.Position, s.TokenText())
case ctext.TextToken:
fmt.Printf("<text> %s: %q\n", s.Position, s.TokenText())
}
}
// Output:
// <comment> hello_world.c:1:1: "/**\n * hello_world.c\n */"
// <text> hello_world.c:3:4: "\n\n#include <stdio.h>\n\nint\nmain( void )\n{\n fputs( \"Hello world\\n\", stdout );\n return 0;\n}\n"
}