Skip to content
Snippets Groups Projects
LexerAddon.cs 2.26 KiB
Newer Older
using System;
using System.IO;
using SimpleScanner;
using ScannerHelper;
using System.Collections.Generic;

namespace  GeneratedLexer
{
    
    public class LexerAddon
    {
        public Scanner myScanner;
        private byte[] inputText = new byte[255];

        public int idCount = 0;
        public int minIdLength = Int32.MaxValue;
        public double avgIdLength = 0;
        public int maxIdLength = 0;
        public int sumInt = 0;
        public double sumDouble = 0.0;
        public List<string> idsInComment = new List<string>();
        

        public LexerAddon(string programText)
        {
            
            using (StreamWriter writer = new StreamWriter(new MemoryStream(inputText)))
            {
                writer.Write(programText);
                writer.Flush();
            }
            
            MemoryStream inputStream = new MemoryStream(inputText);
            
            myScanner = new Scanner(inputStream);
        }

        public void Lex()
        {
            //         3.14 (  3,14    Culture)
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

            int tok = 0;
            do {
                tok = myScanner.yylex();

                if (tok == (int)Tok.EOF)
                {
lusparon332's avatar
lusparon332 committed
                    idsInComment = myScanner.idsInComment;
lusparon332's avatar
lusparon332 committed
                
                else if (tok == (int)Tok.ID)
                {
                    ++idCount;

                    if (myScanner.yytext.Length < minIdLength)
                        minIdLength = myScanner.yytext.Length;
                    if (myScanner.yytext.Length > maxIdLength)
                        maxIdLength = myScanner.yytext.Length;

                    avgIdLength = (avgIdLength * (idCount - 1) + myScanner.yytext.Length) / idCount;
                }

                else if (tok == (int)Tok.RNUM)
                {
                    sumDouble += double.Parse(myScanner.yytext);
                }

                else if (tok == (int)Tok.INUM)
                {
                    sumInt += int.Parse(myScanner.yytext);
                }