{"id":18057747,"url":"https://github.com/safememoryzone/slex","last_synced_at":"2025-04-05T10:42:17.911Z","repository":{"id":244984442,"uuid":"816640183","full_name":"SafeMemoryZone/slex","owner":"SafeMemoryZone","description":"C lexer written in C.","archived":false,"fork":false,"pushed_at":"2024-07-11T15:36:53.000Z","size":46,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-10T21:36:48.436Z","etag":null,"topics":["c","lexer","lexical-analysis"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SafeMemoryZone.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-18T06:17:00.000Z","updated_at":"2024-07-15T10:24:54.000Z","dependencies_parsed_at":"2024-07-11T17:58:53.523Z","dependency_job_id":null,"html_url":"https://github.com/SafeMemoryZone/slex","commit_stats":null,"previous_names":["safememoryzone/slex"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SafeMemoryZone%2Fslex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SafeMemoryZone%2Fslex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SafeMemoryZone%2Fslex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SafeMemoryZone%2Fslex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SafeMemoryZone","download_url":"https://codeload.github.com/SafeMemoryZone/slex/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247325646,"owners_count":20920713,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","lexer","lexical-analysis"],"created_at":"2024-10-31T02:22:36.804Z","updated_at":"2025-04-05T10:42:17.886Z","avatar_url":"https://github.com/SafeMemoryZone.png","language":"C","readme":"# slex\n\nA simple C lexer written in C, inspired by [stb_c_lexer](https://github.com/nothings/stb/blob/master/stb_c_lexer.h).\n\n### Note\n\n\"slex\" stands for \"safe lexer\" because it is designed to handle any input without crashing.\n\n## Features\n\n- Single header file for easy integration.\n- Simple and user-friendly API.\n- No dependencies on the standard library.\n- Robust handling of malformed input to prevent crashes.\n\n## Example/Quickstart\n\n```c\n#include \u003cstdio.h\u003e\n#include \u003cstdlib.h\u003e\n\n/* This macro must be defined in one file (translation unit)\n   to avoid ODR (One Definition Rule) violations.\n*/\n#define SLEX_IMPLEMENTATION\n#include \"slex.h\"\n\nint main(int argc, char** argv) {\n  // Open and read a sample C file \n  FILE *f = fopen(\"sample.c\", \"rb\");\n  char *text = (char *)malloc(1 \u003c\u003c 20);  // Allocate 1 MB for the file content\n  int len = f ? (int)fread(text, 1, 1 \u003c\u003c 20, f) : -1;\n\n  if (len \u003c 0) {\n    fprintf(stderr, \"Error opening file\\n\");\n    free(text);\n    if (f) fclose(f);\n    return 1;\n  }\n\n  fclose(f);\n\n  /* Fields you'll use are:\n     ctx.tok_ty: holds the parsed token type\n     ctx.first_tok_char: pointer to the first character in a token\n     ctx.last_tok_char: pointer to the last character in a token\n     ctx.str_len: if a string or character literal was parsed, it holds the length in bytes\n     ctx.parsed_int_lit: if an integer literal was parsed, it holds the parsed integer value (excluding the '-')\n     ctx.parsed_float_lit: if a float literal was parsed, it holds the parsed float value (excluding the '-')\n  */\n  SlexContext ctx;\n\n  // Storage used by the lexer to store parsed strings and characters (recommended to be 1024+ bytes)\n  // NOTE: Every string or character literal is UTF-8 encoded\n  char store[1024];\n\n  // Initialize the lexer context\n  slex_init_context(\u0026ctx, text, text + len, store, 1024);\n\n  // The lexer only depends on ctx.parse_point and ctx.stream_end.\n\n  /* slex_get_next_token returns 1 if a token was parsed, otherwise 0.\n     When an error occurs, ctx.tok_ty indicates the kind of error,\n     and ctx.parse_point points to the location of the error, while\n     ctx.first_tok_char points to where it began parsing.\n     You can then use slex_get_parse_ptr_location to get the error location.\n  */\n  while (slex_get_next_token(\u0026ctx)) {\n    // Process tokens here\n  }\n\n  free(text);\n  return 0;\n}\n```\n\n## License\n\nThis project is licensed under the MIT License. \n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsafememoryzone%2Fslex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsafememoryzone%2Fslex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsafememoryzone%2Fslex/lists"}