start heredoc

This commit is contained in:
vadim 2017-11-16 16:50:55 +02:00
parent 17f05837ea
commit 25dfb3f277
3 changed files with 1750 additions and 1676 deletions

View File

@ -1,79 +0,0 @@
%{
// Copyright (c) 2015 The golex Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This is an example program using golex run time library.
package main
import (
"bufio"
"go/token"
"io"
"unicode"
"github.com/cznic/golex/lex"
)
// Allocate Character classes anywhere in [0x80, 0xFF].
const (
classUnicodeLeter = iota + 0x80
classUnicodeDigit
classOther
)
type lexer struct {
*lex.Lexer
}
func rune2Class(r rune) int {
if r >= 0 && r < 0x80 { // Keep ASCII as it is.
return int(r)
}
if unicode.IsLetter(r) {
return classUnicodeLeter
}
if unicode.IsDigit(r) {
return classUnicodeDigit
}
return classOther
}
func newLexer(src io.Reader, dst io.Writer, fName string) *lexer {
file := token.NewFileSet().AddFile(fName, -1, 1<<31-1)
lx, err := lex.New(file, bufio.NewReader(src), lex.RuneClass(rune2Class))
if (err != nil) { panic(err) }
return &lexer{lx}
}
func (l *lexer) Lex(lval *yySymType) int {
c := l.Enter()
%}
%yyc c
%yyn c = l.Next()
%yym l.Mark()
digit [0-9]|{unicodeDigit}
identifier {letter}({letter}|{digit})*
int {digit}+
letter [_a-zA-Z]|{unicodeLetter}
unicodeDigit \x81
unicodeLetter \x80
op [-+*/]
%%
c = l.Rule0()
[ \t\r\n]+
[/][/][^\n]+ lval.token = string(l.TokenBytes(nil)); return COMMENT
func lval.token = string(l.TokenBytes(nil)); return FUNC
{identifier} lval.token = string(l.TokenBytes(nil)); return IDENT
{int} lval.token = string(l.TokenBytes(nil)); return INT
{op} lval.token = string(l.TokenBytes(nil)); return OP
%%
if c, ok := l.Abort(); ok { return int(c) }
goto yyAction
}

File diff suppressed because it is too large Load Diff

View File

@ -142,6 +142,7 @@ BNUM 0b[01]+
EXPONENT_DNUM (({LNUM}|{DNUM})[eE][+-]?{LNUM}) EXPONENT_DNUM (({LNUM}|{DNUM})[eE][+-]?{LNUM})
VAR_NAME [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]* VAR_NAME [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*
OPERATORS [;:,.\[\]()|\/\^&\+-*=%!~$<>?@] OPERATORS [;:,.\[\]()|\/\^&\+-*=%!~$<>?@]
NEW_LINE (\r|\n|\r\n)
%% %%
c = l.Rule0() c = l.Rule0()
@ -256,7 +257,7 @@ OPERATORS [;:,.\[\]()|\/\^&\+-*=%!~$<>?@]
<PHP>\*\* fmt.Println("T_POW") <PHP>\*\* fmt.Println("T_POW")
<PHP>\<\< fmt.Println("T_SL") <PHP>\<\< fmt.Println("T_SL")
<PHP>\>\> fmt.Println("T_SR") <PHP>\>\> fmt.Println("T_SR")
<PHP>(#|[/][/])[^\n]+ fmt.Println("T_COMMENT"); // TODO: handle \r\n and allow ?> <PHP>(#|[/][/]){NEW_LINE} fmt.Println("T_COMMENT"); // TODO: handle \r\n and allow ?>
<PHP>'[^']*(\\')*' fmt.Println("T_CONSTANT_ENCAPSED_STRING") <PHP>'[^']*(\\')*' fmt.Println("T_CONSTANT_ENCAPSED_STRING")
<PHP>{OPERATORS} fmt.Printf("%s\n", l.TokenBytes(nil)); <PHP>{OPERATORS} fmt.Printf("%s\n", l.TokenBytes(nil));
@ -270,6 +271,16 @@ OPERATORS [;:,.\[\]()|\/\^&\+-*=%!~$<>?@]
<PROPERTY>. l.ungetN(1);begin(PHP) <PROPERTY>. l.ungetN(1);begin(PHP)
<PHP>[\']([^\\\']*([\\][\'])*)*[\'] fmt.Printf("T_CONSTANT_ENCAPSED_STRING: %s\n", l.TokenBytes(nil)); <PHP>[\']([^\\\']*([\\][\'])*)*[\'] fmt.Printf("T_CONSTANT_ENCAPSED_STRING: %s\n", l.TokenBytes(nil));
<PHP>[b]?\<\<\<[ \t]*({VAR_NAME}|([']{VAR_NAME}['])|(["]{VAR_NAME}["])){NEW_LINE}
tb := l.TokenBytes(nil)
fmt.Println(string(tb[len(tb)-2]))
// switch la {
// case '\'' : fmt.Println("ST_NOWDOC");//begin(ST_NOWDOC)
// case '"' : fmt.Println("ST_HEREDOC");//begin(ST_HEREDOC)
// }
fmt.Printf("T_START_HEREDOC: %s\n", l.TokenBytes(nil));
<PHP>[b]?[\"] <PHP>[b]?[\"]
binPrefix := l.TokenBytes(nil)[0] == 'b' binPrefix := l.TokenBytes(nil)[0] == 'b'